fork(3) download
  1. #include <algorithm>
  2. #include <cctype>
  3. #include <cmath>
  4. #include <cstdio>
  5. #include <cstdlib>
  6. #include <cstring>
  7. #include <iostream>
  8. #include <map>
  9. #include <queue>
  10. #include <set>
  11. #include <sstream>
  12. #include <stack>
  13. #include <string>
  14. #include <vector>
  15.  
  16. #define EPS 1e-11
  17. #define inf ( 1LL << 31 ) - 1
  18. #define LL long long
  19.  
  20. #define abs(x) (((x)< 0) ? (-(x)) : (x))
  21. #define all(x) (x).begin(), (x).end()
  22. #define ms(x, a) memset((x), (a), sizeof(x))
  23.  
  24. #define mp make_pair
  25. #define pb push_back
  26. #define sz(k) (int)(k).size()
  27.  
  28. using namespace std;
  29.  
  30. typedef vector <int> vi;
  31.  
  32. bool isPalindrome (string str)
  33. {
  34. int start = 0, end = str.length() - 1;
  35. while (start < end)
  36. {
  37. if (str[start++] != str[end--])
  38. {
  39. return false;
  40. }
  41. }
  42. return true;
  43. }
  44.  
  45. int get_bigger (string str1, string str2)
  46. {
  47. int len = str1.length();
  48. for (int i = 0; i < len; i++)
  49. {
  50. if (str1[i] < str2[i])
  51. {
  52. return -1;
  53. }
  54. else if (str1[i] > str2[i])
  55. {
  56. return 1;
  57. }
  58. }
  59. return 0;
  60. }
  61.  
  62. string rev (string str)
  63. {
  64. string revstr = str;
  65. int start = 0, end = str.size() - 1;
  66. while (start <= end)
  67. {
  68. revstr[end] = str[start];
  69. revstr[start++] = str[end--];
  70. }
  71. return revstr;
  72. }
  73.  
  74. string inc_str (string str)
  75. {
  76. int len = str.size() - 1;
  77. //cout << "Len " << len << endl;
  78. string res = str;
  79. int carry = 1;
  80. while (len >= 0 && carry)
  81. {
  82. if (str[len] == '9')
  83. {
  84. res[len--] = '0';
  85. }
  86. else
  87. {
  88. res[len--]++;
  89. //res.insert (res.begin(), ++X);
  90. //cout << "X is " << X << " and Len is " << len << endl;
  91. carry = 0;
  92. }
  93. }
  94. if (carry)
  95. {
  96. res = "1" + res;
  97. }
  98. return res;
  99. }
  100.  
  101. void process (string str)
  102. {
  103. string str1, str2, str3;
  104. int len = str.size();
  105. str1 = str.substr (0, len/2);
  106. if (len % 2 == 0)
  107. {
  108. str2 = "";
  109. str3 = str.substr (len/2, len/2);
  110. //cout << "Even " << str1 << " " << str2 << " " << str3 << endl;
  111. }
  112. else
  113. {
  114. str2 = str.substr (len/2, 1);
  115. str3 = str.substr (len/2 + 1, len/2);
  116. //cout << "Odd " << str1 << " " << str2 << " " << str3 << endl;
  117. }
  118. string rev_str1 = rev (str1);
  119. //cout << "After Reverse " << str1 << " " << rev_str1 << endl;
  120. int rc = 0;
  121. //if (get_bigger (rev_str1, str3) > 0)
  122. if (rev_str1 > str3)
  123. {
  124. //cout << "Reversed First String is bigger" << endl;
  125. cout << str1 + str2 + rev_str1 << endl;
  126. return;
  127. }
  128. if (str2 == "")
  129. {
  130. //cout << "Middle String is Empty" << endl;
  131. int ix = str1.size();
  132. rev_str1 = "";
  133. rev_str1.append (ix, '0');
  134. str1 = inc_str (str1);
  135. }
  136. else
  137. {
  138. //cout << "Middle String is " << str2 << endl;
  139. str2 = inc_str (str2);
  140. //cout << "Incremented String is " << str2 << endl;
  141. if (str2.size() > 1)
  142. {
  143. int ix = str1.size();
  144. rev_str1 = "";
  145. rev_str1.append (ix, '0');
  146. str1 = inc_str (str1);
  147. str2 = "0";
  148. }
  149. }
  150. //cout << "At the end of Iteration " << str1 + str2 + rev_str1 << endl;
  151. string str4 = str1 + str2 + rev_str1;
  152. if (isPalindrome(str4))
  153. {
  154. cout << str4 << endl;
  155. return;
  156. }
  157. process (str4);
  158. }
  159.  
  160. int main ()
  161. {
  162. int total ;
  163. cin >> total;
  164. string str;
  165. while (total-- > 0)
  166. {
  167. cin >> str;
  168. //cout << "Processing " << str << endl;
  169. process (str);
  170. }
  171. return 0;
  172. }
  173.  
Success #stdin #stdout 0.01s 2824KB
stdin
6
808
2133
1
999
4599954
45874
stdout
818
2222
2
1001
4600064
45954