fork download
  1. #include <iostream>
  2. #include <cstring>
  3. #include <string>
  4. #include <vector>
  5. #include <sstream>
  6. using namespace std;
  7.  
  8. void printArray(vector<int> a){
  9. cout<<"printing the array - ";
  10. for(int i : a)
  11. cout<<i<<" ";
  12.  
  13. cout<<endl;
  14. }
  15.  
  16.  
  17. string biggerPalin(string x)
  18. {
  19. int n = x.size();
  20. int idx, i;
  21. if (n & 1)
  22. {
  23. if (x[n / 2] != '9')
  24. {
  25. int a = (x[n / 2] - '0') + 1;
  26. // cout<<a<<endl;
  27. x.replace(n/2, 1, to_string(a));
  28. // cout<<x[n/2]<<endl;
  29. // cout<<x<<endl;
  30. }
  31. else
  32. {
  33. // x[n / 2] = '0';
  34. x.replace(n/2, 1, to_string(0));
  35. idx = n / 2;
  36. i = 1;
  37. while (x[idx - i] == '9' && idx - i >= 0)
  38. {
  39. // x[idx - i] = '0';
  40. x.replace(idx-i, 1, to_string(0));
  41. // x[idx + i] = '0';
  42. x.replace(idx+i, 1, to_string(0));
  43. i++;
  44. }
  45. if (x[0] == '0')
  46. {
  47. // x[n - 1] = '1';
  48. x.replace(n-1, 1, to_string(1));
  49. x = '1' + x;
  50. }
  51. else{
  52. x.replace(idx-i, 1, to_string((x[idx-i]-'0') + 1));
  53. // x[idx-i] = (x[idx-i] - '0') + 1;
  54. // x[idx+i] = (x[idx+i] - '0') + 1;
  55. x.replace(idx+i, 1, to_string((x[idx+i]-'0') + 1));
  56. }
  57. }
  58. }
  59. else
  60. {
  61. if (x[(n - 1) / 2] != '9')
  62. {
  63. // x[(n - 1) / 2] = (x[(n - 1) / 2] - '0') + 1;
  64. x.replace((n-1)/2, 1, to_string((x[(n - 1) / 2] - '0') + 1));
  65. // x[n / 2] = (x[n / 2] - '0') + 1;
  66. x.replace(n/2, 1, to_string((x[n / 2] - '0') + 1));
  67. }
  68.  
  69. else
  70. {
  71. idx = (n - 1) / 2;
  72. i = 0;
  73. while (x[idx - i] == '9' && idx - i >= 0)
  74. {
  75. // x[idx - i] = '0';
  76. x.replace(idx-i, 1, to_string(0));
  77. i++;
  78. x.replace(idx+i, 1, to_string(0));
  79. // x[idx + i] = '0';
  80. }
  81. if (x[0] == '0')
  82. {
  83. // x[n - 1] = '1';
  84. x.replace(n-1, 1, to_string(1));
  85. x = '1' + x;
  86. }
  87. else{
  88. // x[idx-i] = (x[idx - i] - '0') + 1;
  89. x.replace(idx-i, 1, to_string((x[idx - i] - '0') + 1));
  90. i++;
  91. x.replace(idx+i, 1, to_string((x[idx + i] - '0') + 1));
  92. // x[idx + i] = (x[idx + i] - '0') + 1;
  93. }
  94. }
  95. }
  96. return x;
  97. }
  98.  
  99. string findPalin(string str)
  100. {
  101. string res = str;
  102. int n = str.size() - 1;
  103. for (int i = 0; i < (n + 1) / 2; i++)
  104. {
  105. str[n - i] = str[i];
  106. }
  107. // cout<<res<<" "<<str<<endl;
  108. if (str > res)
  109. return str;
  110. else
  111. {
  112. return biggerPalin(str);
  113. }
  114. }
  115.  
  116. int main()
  117. {
  118. int t;
  119. cin >> t;
  120. while (t > 0)
  121. {
  122. string str;
  123. cin >> str;
  124. // long long int num = findPalin(str);
  125. cout << findPalin(str) << endl;
  126. t--;
  127. }
  128. }
Success #stdin #stdout 0s 4184KB
stdin
9
101
299
3999
9999999
15243
912376589716325
123456789
111
123454321
stdout
111
303
4004
10000001
15251
912376595673219
123464321
121
123464321