fork(3) download
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <iterator>
  4. #include <string>
  5.  
  6. bool isPalindrome(std::string::const_iterator& start, std::string::const_iterator& end)
  7. {
  8. while (start < end) {
  9. --end;
  10. if (*start != *end) {
  11. return false;
  12. }
  13. ++start;
  14. }
  15. return true;
  16. }
  17.  
  18. bool test(const std::string& s)
  19. {
  20. auto start = s.begin();
  21. auto end = s.end();
  22.  
  23. if (isPalindrome(start, end)) {
  24. // If we remove the middle character of a palindrome,
  25. // We still have a palindrome.
  26. return true;
  27. }
  28. // Now test if there is a palindrome
  29. // if we skip the mismatch char from the start or from the end.
  30. auto start2 = start;
  31. auto end2 = end;
  32. ++start2;
  33. --end;
  34. return isPalindrome(start, end) || isPalindrome(start2, end2);
  35. }
  36.  
  37. int main()
  38. {
  39. for (const auto& s : {"aba", "aa", "abccdba", "abdccba", "abcd"}) {
  40. std::cout << s << " " << std::boolalpha << test(s) << std::endl;
  41. }
  42.  
  43. }
  44.  
Success #stdin #stdout 0s 3428KB
stdin
Standard input is empty
stdout
aba true
aa true
abccdba true
abdccba true
abcd false