fork(2) download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4. int F,L;
  5. int FindMatch(int firstPos, int lastPos, string str)
  6. {
  7. //cout << "First Position:\n" << firstPos <<"\n";
  8. //cout << "Last Position:\n" << lastPos <<"\n";
  9.  
  10. if (lastPos >= firstPos)
  11. {
  12. if (lastPos < str.length()/2)
  13. {
  14. return FindMatch(firstPos + 1, firstPos + str.length()/2 - 1, str);
  15. }
  16. if (firstPos == lastPos)
  17. {
  18. //cout<<"HERE\n";
  19.  
  20. return 0;
  21. }
  22.  
  23. else if (str[firstPos] == str[lastPos])
  24. {
  25. //cout << "firstPos\n" << str[firstPos]<<"\n";
  26. //cout<<"LastPos\n" <<str[lastPos]<<"\n";
  27.  
  28. return 1 + FindMatch(firstPos + 1,lastPos - 1,str);
  29. }
  30. else
  31. {
  32. //cout<<"else firstPos\n" <<str[firstPos]<<"\n";
  33. //cout<<"else LastPos\n" <<str[lastPos]<<"\n";
  34. return FindMatch(firstPos,lastPos - 1,str);
  35. }
  36. }
  37. else
  38. {
  39. //cout<<"NO POINT";
  40. //cout<<"firstPos" <<str[firstPos];
  41. //cout<<"LastPos" <<str[lastPos];
  42. return 0;
  43. }
  44. }
  45.  
  46. int IsPalin(string testStr)
  47. {
  48. int matchcount = FindMatch(0, testStr.length() - 1, testStr);
  49. int exp_match = (testStr.length() - 1)/ 2;
  50. if (matchcount >= exp_match) {
  51. return true;
  52. }
  53. else
  54. {
  55. return false;
  56. }
  57. }
  58.  
  59.  
  60. int main() {
  61.  
  62. // Enter number of test cases
  63. // Enter the string for each test case
  64. // Find the number of letters in the string
  65. // If even then
  66. // Find iteratively if there is a match between first and the last letter
  67. // If the count of mismatch is only one then give the answer true
  68. // If odd then
  69. // Do same thing , just consider the middle character in itself as a match.
  70.  
  71.  
  72.  
  73. int totTC = 0;
  74. // cout<< "Enter the number of test cases:\n";
  75. cin>>totTC;
  76. int counter = 0;
  77. while (counter != totTC)
  78. {
  79. string test;
  80. cin>> test;
  81. if ( IsPalin(test) )
  82. {
  83. cout<<"YES\n";
  84. }
  85. else
  86. {
  87. cout<<"NO\n";
  88. }
  89. counter++;
  90. }
  91. return 0;
  92. }
Success #stdin #stdout 0s 3100KB
stdin
Standard input is empty
stdout
Standard output is empty