描述:从一个字符串中查找最长的回文子串,并输出该子串。
样例输入:dgaabcddcbadfabcdefghijkkjihgfedcbagaaabbccddddccbbaaaf
样例输出:aaabbccddddccbbaaa
#include <string>
#include <iostream>
using namespace std;
static string FindSymmetry(string str)
{
for (int i = str.length() - 1; i >= 0; i--)
{
for (int j = 0; j < i; j++)
{
string temp = str.substr(j, i - j+1);
int k = 0;
for (; k < temp.length() / 2; k++)
{
if (temp[k] != temp[temp.length() - k - 1])
break;
}
if (k == temp.length() / 2)
{
return temp;
}
}
}
return "";
}
int main()
{
string str = "dgaabcddcbadfabcdefghijkkjihgfedcbagaaabbccddddccbbaaaf";
cout<<FindSymmetry(str)<<endl;
return 0;
}