fork download
  1. /*
  2.   spoj PALIN
  3.   Shubham Ivane
  4.   Shri G.S. Institute of Technology and Science, Indore
  5.   @shubhamivane
  6. */
  7. #include<iostream>
  8. #include<list>
  9. using namespace std;
  10. void printList(const list<int>& li)
  11. {
  12. list<int>::const_iterator itr;
  13. for(itr = li.begin(); itr != li.end() ; itr++)
  14. {
  15. cout << *itr;
  16. }
  17. cout << "\n";
  18. }
  19. void convertIntoList(list<int>& li,string& num)
  20. {
  21. for(int i = 0 ; i < num.size() ; i++)
  22. {
  23. li.push_front(num[i]-48);
  24. }
  25. }
  26. void increment(list<int>& li)
  27. {
  28. list<int>::iterator itr = li.begin();
  29. //cout << *itr;
  30. *itr = *itr + 1;
  31. while(true)
  32. {
  33. if(*itr >= 10)
  34. {
  35. *itr = *itr % 10;
  36. itr++;
  37. if(itr == li.end())
  38. li.push_back(1);
  39. else
  40. *itr += 1;
  41. }
  42. else
  43. {
  44. break;
  45. }
  46. }
  47. }
  48. bool checkPalindrome(list<int>& li)
  49. {
  50. list<int>::iterator itr = li.begin();
  51. list<int>::reverse_iterator ritr = li.rbegin();
  52. for(;itr!=li.end();itr++,++ritr)
  53. {
  54. if(*itr != *ritr)return false;
  55. }
  56. return true;
  57. }
  58. void nextPalin(string& num)
  59. {
  60. list<int> li;
  61. convertIntoList(li,num);
  62. //printList(li);
  63. while(true)
  64. {
  65. increment(li);
  66. //printList(li);
  67. if(checkPalindrome(li))
  68. {
  69. printList(li);
  70. break;
  71. }
  72. }
  73. }
  74. int main()
  75. {
  76. int t;
  77. string num;
  78. cin >> t;
  79. while(t--)
  80. {
  81. cin >> num;
  82. nextPalin(num);
  83. }
  84. }
Success #stdin #stdout 0s 16056KB
stdin
2
100
99
stdout
101
101