动态规划
class Solution { public: string longestPalindrome(string s) { int dp[1100][1100]; int len=s.size(),ans=1; int left=0,right=0; for(int i=0;i<len;i++){ for(int j=i+1;j<len;j++){ dp[i][j]=0; } dp[i][i]=1; if(s[i]==s[i+1]){ dp[i][i+1]=1; ans=2; left=i;right=i+1; } } for(int L=3;L<=len;L++){ for(int i=0;i+L-1<len;i++){ int j=i+L-1; //cout<<i<<", "<<j<<endl; if(s[i]==s[j]&&dp[i+1][j-1]==1){ dp[i][j]=1; ans=L; left=i;right=j; } } } //cout<<left<<right<<ans<<endl; return s.substr(left,right-left+1); } };