Guest User

Untitled

a guest
Mar 23rd, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. char k[1000005];
  5.  
  6. int l,i,j,mid;
  7.  
  8. bool chkNine()
  9. {
  10.  
  11. cout<<""; //for SPOJ's internal problem, this line is needed
  12. for(i=0;i<l;i++)
  13. {
  14. if( k[i]!='9' ) return false;
  15. }
  16. }
  17.  
  18. void nextPalin()
  19. {
  20.  
  21. int f=0;
  22. l=strlen(k);
  23.  
  24. if(chkNine()) {
  25. memset(k,'0',l);
  26. k[0] ='1';
  27. k[l]='1';
  28. k[l+1]='\0';
  29. return;
  30. }
  31.  
  32. // calc mid,i,j
  33. if(l%2==0)
  34. {
  35. i= ( l/2 )-1;
  36. j= l/2;
  37. mid= (l/2) -1;
  38. }
  39. else
  40. {
  41. i= ( l/2 )-1;
  42. j= (l/2 )+1;
  43. mid=l/2;
  44. }
  45. int a = i, b =j;
  46. //iteration of k[i] & k[j]
  47. for(; i>=0; i--,j++)
  48. {
  49. if(k[i] > k[j])
  50. {
  51. f=1;
  52. break;
  53. }
  54. else if ( k[i] < k[j] )
  55. {
  56. f=2;
  57. break;
  58. }
  59. }
  60. if(f==1)
  61. {
  62. for(; i>=0; i--,j++)
  63. {
  64. k[j]=k[i];
  65. }
  66. }
  67. else if(f==2||f==0)
  68. {
  69. int temp,c=1;
  70. if(l%2==0)
  71. {
  72. for(i=a,j=b; i>=0; i--,j++)
  73. {
  74. temp = (k[i]-48)+c ;
  75. c= temp /10;
  76. temp= temp%10;
  77. k[i]= temp+48;
  78. k[j]=k[i];
  79. }
  80. }
  81. else if(l%2!=0)
  82. {
  83. temp = (k[mid]-48)+c ;
  84. c= temp /10;
  85. temp= temp%10;
  86. k[mid]= temp+48;
  87.  
  88. for(i=a,j=b; i>=0; i--,j++)
  89. {
  90. temp = (k[i]-48)+c ;
  91. c= temp /10;
  92. temp= temp%10;
  93. k[i]= temp+48;
  94. k[j]=k[i];
  95. }
  96. }
  97. }
  98.  
  99. }
  100.  
  101. int main()
  102. {
  103. int t;
  104. scanf("%d",&t);
  105. getchar();
  106. while(t--)
  107. {
  108. scanf("%[^\n]%*c",k);
  109. nextPalin();
  110. printf("%s\n",k);
  111. }
  112.  
  113. return 0;
  114. }
Add Comment
Please, Sign In to add comment