fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <map>
  4. #include <algorithm>
  5. #include <string>
  6. using namespace std;
  7.  
  8. #define CASES int t;cin>>t;while(t--)
  9. #define REP(i, val, b) for (int i = val; i < b; ++i)
  10.  
  11. class stringMod
  12. {
  13. public:
  14. string s;
  15. //Here is the increment frunction.
  16. void increment(int pos)
  17. {
  18. s[pos]++; //We have incremented right most digit by "1"
  19. /*
  20. When the right most digit is "9" and you are trying to increment it by "1", then that
  21. will become ":" in cpp. So we have checked ":" in condition here.
  22. */
  23. if (s[pos] == ':')
  24. {
  25. //First we will make that position to "0".
  26.  
  27. //Here is the explanation with example. if the number is 139 and we will do s[2]++.
  28. //Then first it will become "13:". So in this line we will make ":" to "0".
  29. //So our number will become "130".
  30. //Now our task is to increment "3" to "4". So we will do increment(pos - 1) here if and only
  31. //if pos > 0. Now you must have question in your mind that what is the meaning of the
  32. //condition if (pos > 0). So here is the explanation. If our number is 139, then our position
  33. //is 2 and we are incrementing our position by 1 so it will become "13:" and after that "130"
  34. //and position is 2 so here position is 2 that is greater than 0 so we will increment
  35. //previous position. So we will increment position 2, so our number will become 140.
  36. //Now we are moving towards to else part. When will these condition happen.
  37. //So if we have a number "99" and we have reached to this part and our number
  38. //have became "9:" and then "90". Now here our position is 1 as we have incremented
  39. //last digit so we will increment 0th position, then
  40. //our number will become ":0" and after that our number will become "00" and after that our
  41. //position is 0 so we have to prepend "1" to our number so our numbber will become "100".
  42. //and this process will run again. So this is the else part explanation.
  43. s[pos] = '0';
  44. if (pos > 0)
  45. {
  46. increment(pos - 1);
  47. }
  48. else
  49. {
  50. s.insert(s.begin(), '1');
  51. }
  52.  
  53. }
  54. }
  55. };
  56.  
  57. int main()
  58. {
  59. CASES{
  60. stringMod s;
  61. cin >> s.s;
  62. int l = s.s.length(); //length of the input || number
  63.  
  64. //First we will increment last digit by "1",
  65. //so that we will check whether the next number is palindrome or not
  66. s.increment(l - 1);
  67. l = s.s.length();
  68. //As I have explained we will loop through half of the length of input.
  69. REP(i, 0, l / 2)
  70. {
  71. int left = i; //left most digit is this from the input || number
  72. int right = (l - 1) - i; //right most digit is thie from input || number
  73. //Will run while loop until left and right number is equal as explained
  74. while (s.s[left] != s.s[right])
  75. {
  76. //If not equal we will increment right most number by "1".
  77. s.increment(right);
  78. }
  79. }
  80. //Here is the final string.
  81. cout << s.s << endl;
  82. }
  83.  
  84. return 0;
  85. }
Success #stdin #stdout 0s 4460KB
stdin
Standard input is empty
stdout
Standard output is empty