#include <iostream>
using namespace std;
bool validStr(string s,int left,int right)
{
while (left < right) {
if(s.at(left) != s.at(right))
return false;
left++;
right--;
}
return true;
}
string longestPalindrome(string s) {
int len = s.length();
int maxLen = 1;
if(len < 2 )
{
cout << s;
return s;
}
string res = s.substr(0,1);
for (int i = 0; i < len - 1 ; i++) {
for (int j = i + 1; j < len; j++) {
if (j - i + 1 > maxLen && validStr(s, i, j)) {
maxLen = j - i + 1;
res = s.substr(i,maxLen);
}
}
}
cout << res;
return res;
}
int main()
{
longestPalindrome("abc");
// longestPalindrome("a");
// longestPalindrome("abab");
// longestPalindrome("abab78987");
// longestPalindrome("aa678");
return 0;
}
|