题解 | #最长回文子串#
最长回文子串
http://www.nowcoder.com/practice/b4525d1d84934cf280439aeecc36f4af
class Solution {
public:
int getLongestPalindrome(string A, int n) {
// write code here
int m=0;
vector<vector<int>> dp(n+1,vector<int>(n+1,0));
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(A[i]==A[n-1-j])
{
dp[i+1][j+1]=dp[i][j]+1;
if(dp[i+1][j+1]>=m && i+j-dp[i+1][j+1]+1==n-1) m=dp[i+1][j+1];
}
else dp[i+1][j+1]=0;
}
}
return m;
}
};
查看26道真题和解析