fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long ll;
  5. typedef pair<ll,ll> ii;
  6.  
  7. #define fill(a,x) memset(a,x,sizeof(a))
  8. #define pb push_back
  9. #define mp make_pair
  10. #define sz(x) (int)x.size()
  11. #define F first
  12. #define S second
  13. #define FOR(i,a,b) for(int i = a; i<=b; ++i)
  14. #define NFOR(i,a,b) for(int i = a; i>=b; --i)
  15. #define fast ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
  16. const ll INF = 1e18;
  17. const ll mod = 1e9+7;
  18. const int N = 1e6+10;
  19. char str[1000005];
  20.  
  21. int main()
  22. {
  23. int t,i,j;
  24.  
  25. scanf("%i",&t);
  26.  
  27. while(t--) {
  28.  
  29. scanf("%s",str);
  30.  
  31. int lenght = strlen(str);
  32.  
  33. j = lenght;
  34. i = -1;
  35.  
  36. while(++i <= --j) {
  37. if(str[i] != str[j]) {
  38. break;
  39. }
  40. }
  41.  
  42. if(j < i) {
  43. /* Number is palindrome */
  44.  
  45. if(lenght & 1) {
  46. /* odd lenght */
  47.  
  48. if(str[lenght/2] < '9'){
  49. /* check the middle one not 9 */
  50.  
  51. str[lenght/2]++;
  52.  
  53. printf("%s\n",str);
  54. }
  55.  
  56. else {
  57. str[lenght/2] = '0';
  58.  
  59. i = lenght/2 - 1;
  60. j = lenght/2 + 1;
  61.  
  62. while(i >= 0) {
  63.  
  64. if(str[i] < '9') {
  65. str[i]++;
  66. str[j]++;
  67. break;
  68. }
  69.  
  70. else {
  71. str[i] = str[j] = '0';
  72. }
  73.  
  74. i--;
  75. j++;
  76. }
  77.  
  78. if(i < 0) {
  79. printf("1");
  80.  
  81. i = lenght;
  82. while(--i > 0) {
  83. printf("0");
  84. }
  85.  
  86. printf("1\n");
  87. }
  88.  
  89. else {
  90.  
  91. printf("%s\n",str);
  92. }
  93. }
  94. }
  95.  
  96. else {
  97. /* even lenght */
  98.  
  99. i = lenght/2 - 1;
  100. j = lenght/2;
  101.  
  102. while(i >= 0) {
  103.  
  104. if(str[i] < '9') {
  105. str[i]++;
  106. str[j]++;
  107. break;
  108. }
  109.  
  110. else {
  111. str[i] = str[j] = '0';
  112. }
  113.  
  114. i--;
  115. j++;
  116. }
  117.  
  118. if(i < 0) {
  119. printf("1");
  120.  
  121. i = lenght;
  122. while(--i > 0) {
  123. printf("0");
  124. }
  125.  
  126. printf("1\n");
  127. }
  128.  
  129. else {
  130.  
  131. printf("%s\n",str);
  132. }
  133. }
  134. }
  135.  
  136. else {
  137. if(lenght & 1) {
  138. i = lenght/2 - 1;
  139. j = lenght/2 + 1;
  140. }
  141. else {
  142. i =lenght/2 - 1;
  143. j = lenght/2;
  144. }
  145.  
  146. int flag;
  147. while(i >= 0) {
  148.  
  149. if(str[i] - str[j] > 0) {
  150. flag = 1;
  151. break;
  152. }
  153.  
  154. else if( str[i] - str[j] < 0 ) {
  155. flag = 2;
  156. break;
  157. }
  158.  
  159. i--;
  160. j++;
  161. }
  162. if(lenght & 1) {
  163. i = lenght/2 - 1;
  164. j = lenght/2 + 1;
  165. }
  166. else {
  167. i = lenght/2 - 1;
  168. j = lenght/2;
  169. }
  170. if(flag == 1) { /* line 1*/
  171. while(i >= 0) {
  172. str[j] = str[i];
  173.  
  174. i--;
  175. j++;
  176. }
  177. }
  178. else if(flag == 2 && lenght&1 && str[lenght/2] < '9'){ /* line 2*/
  179. str[lenght/2]++;
  180.  
  181. i = lenght/2 - 1;
  182. j = lenght/2 + 1;
  183.  
  184. while(i >= 0) {
  185. str[j] = str[i];
  186. i--;
  187. j++;
  188. }
  189. }
  190. else { /* line 3 */
  191.  
  192. if( lenght & 1) {
  193. str[lenght/2] = '0';
  194. }
  195. while(i >= 0) {
  196. if(str[i] < '9') {
  197. str[i]++;
  198. str[j] = str[i];
  199. break;
  200. }
  201. else {
  202. str[i] = str[j] = '0';
  203. }
  204. i--;
  205. j++;
  206. }
  207.  
  208. while(i >= 0) {
  209. str[j] = str[i];
  210.  
  211. i--;
  212. j++;
  213. }
  214. }
  215. printf("%s\n",str);
  216. }
  217. }
  218. return 0;
  219. }
Success #stdin #stdout 0s 16216KB
stdin
1
99
stdout
101