给定一个字符串,求出其中的最长回文子串。
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
int LongestPalindromicSubstring(string a){
int size=a.length();
vector<vector<int>>dp(size,vector<int>(size,1));
int res=0;
for(int step=1;step<size;step++){
for(int j=0;j+step<size;j++){
if(step==1){
if(a[j]==a[j+step]){
dp[j][j+step]=step+1;
res = max(res,step+1);
}
else{
dp[j][j+step]=0;
}
}
else{
if(a[j]==a[j+step]&&dp[j+1][j+step-1]){
dp[j][j+step]=step+1;
res = max(res,step+1);
}
else{
dp[j][j+step]=0;
}
}
}
}//for
return res;
}
int main(){
string a;
while(getline(cin,a)){
cout<<LongestPalindromicSubstring(a)<<endl;
}
}