fork download
  1. //
  2. // palin5.c
  3. // Spoj.pl
  4. //
  5. // Created by Subodh Kamble on 17/04/13.
  6. // Copyright (c) 2013 Subodh Kamble. All rights reserved.
  7. //
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12.  
  13. int main()
  14. {
  15. int t , i;
  16. char *input , *output ;
  17. unsigned long long int l , x , y , mid ;
  18. int NoPalindrome = 1 ;
  19. scanf("%d",&t);
  20.  
  21. while (t--)
  22. {
  23. input = (char *)malloc(1000000);
  24. scanf("%s",input);
  25. l = strlen(input);
  26.  
  27. NoPalindrome = 1 ;
  28. i = 0 ;
  29. for(i = 0 ; i< l ; i++)
  30. {
  31. if(input[i]!='9')
  32. break;
  33. }
  34.  
  35. if(i==l)
  36. {
  37. output = malloc(l+2);
  38. output[0]='1';
  39. output[l]='1';
  40. for(i=1;i<l;i++)
  41. output[i]='0';
  42. output[l+1]='\0';
  43. printf("%s\n",output);
  44. break;
  45. }
  46.  
  47.  
  48. //make a string containig all digits of input
  49. output = malloc(l);
  50. strcpy(output,input);
  51.  
  52. //find the mid number
  53. l = strlen(output);
  54. output[l-1]=output[l-1]+1;
  55. if (l%2==0)
  56. {
  57. //even
  58. mid = l/2 ;
  59. x = mid - 1 ;
  60. y = mid ;
  61.  
  62. while (y!=l)
  63. {
  64. if (output[x] < output[y])
  65. {
  66. output[x]=output[x]+1 ;
  67. output[y]=output[x];
  68. break ;
  69.  
  70. }
  71. else if (output[x] == output[y] && output[x] != '\0')
  72. {
  73. x--;
  74. y++;
  75. if(output[x] < output[y])
  76. {
  77. output[x+1] = output[x+1]+1;
  78. output[y-1] = output[y-1]+1;
  79. }
  80. }
  81. else if (output[x] == '9' && output[y]== '9')
  82. {
  83. while (output[x]== output[y])
  84. {
  85. x--;
  86. y++;
  87.  
  88. }
  89.  
  90. if (output[x] < output[y]) // 1997 case
  91. {
  92. output[x]=output[x]+1;
  93. output[y]=output[x];
  94. for (i=x+1; i<=y-1; i++)
  95. output[i]='0';
  96. }
  97. break;
  98. }
  99. else if (output[x] > output[y])
  100. {
  101. output[y]=output[x];
  102. break ;
  103. }
  104.  
  105. x--;
  106. y++;
  107. }
  108.  
  109. while (y <= l-1 )
  110. {
  111. output[y]=output[x];
  112. x--;
  113. y++;
  114. }
  115.  
  116. }
  117. else
  118. {
  119. //odd
  120. mid = l/2 ;
  121. x = mid - 1 ;
  122. y = mid + 1;
  123.  
  124. while (y!=l)
  125. {
  126. if (output[x] <= output[y] && output[mid] != '9')
  127. {
  128. output[mid]=output[mid]+1;
  129. break ;
  130. }
  131. else if (output[x] <= output[y] && output[mid] == '9')
  132. {
  133. while (output[x] == output[y])
  134. {
  135. x--;
  136. y++;
  137.  
  138. }
  139.  
  140. if (output[x] < output[y]) // 14897 case
  141. {
  142. output[x]=output[x]+1;
  143. output[y]=output[x];
  144. for (i=x+1; i<=y-1; i++)
  145. output[i]='0';
  146. }
  147. break;
  148. }
  149. else if (output[x] > output[y])
  150. {
  151. output[y]=output[x];
  152. break ;
  153. }
  154.  
  155. x--;
  156. y++;
  157. }
  158.  
  159. while (y <= l-1 )
  160. {
  161. output[y]=output[x];
  162. x--;
  163. y++;
  164. }
  165.  
  166.  
  167.  
  168. }
  169.  
  170.  
  171.  
  172. //check
  173. x = 0 ;
  174. y = l -1 ;
  175. while(output[x]==output[y] && output[x] != '\0')
  176. {
  177. x++;
  178. y--;
  179. }
  180. if(x-1 == l-1)
  181. {
  182. NoPalindrome = 0 ;
  183. printf("%s\n",output);
  184. }
  185.  
  186.  
  187.  
  188.  
  189. }
  190.  
  191. free(input);
  192. free(output);
  193.  
  194. return 0 ;
  195. }
  196.  
Success #stdin #stdout 0s 1968KB
stdin
45
999999
stdout
1000001