# Leetcode_5 solve Longest Palindromic Substring

Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.

Example 1:

Output: “bab”
Note: “aba” is also a valid answer.

Example 2:

Input: “cbbd”
Output: “bb”

class Solution {
public:
string longestPalindrome(string s)
{
int l,r,max_length=1;
int dp[1000+10][1000+10];
memset(dp,0,sizeof(dp));
int len = s.length();
for(int i=0;i<len;i++)  dp[i][i] = 1;
for(int i=0;i<len-1;i++)
{
if(s[i]==s[i+1])
{
l = i;
r = i+1;
max_length =2 ;
dp[i][i+1] = 2;
}
}
int left=0,right=0;
for(int i=0;i<len;i++)
{
if(s[i]==s[i+1]&&i+1<len)longestPalindrome
{
left = i-1;
right = i+2;
while(left>=0&&right<len&&s[left]==s[right])
{
dp[left][right] = dp[left+1][right-1]+2;
if(dp[left][right]>max_length)
{
l = left;
r = right;
max_length = dp[left][right];
}
left--;
right++;
}
}
if(s[i-1]==s[i+1]&&i+1<len&&i-1>=0)
{
left = i-1;
right = i+1;
while(left>=0&&right<len&&s[left]==s[right])
{
dp[left][right] = dp[left+1][right-1]+2;
if(dp[left][right]>max_length)
{
l = left;
r = right;
max_length = dp[left][right];
}
left--;
right++;
}
}
}
string ans;
if(max_length==1)  ans.push_back(s[0]);
else
for(int i=l;i<=r;i++) ans.push_back(s[i]);
return ans;
}
};

dp[j][i]= 1 表示j和i之间是回文串，dp[j][i]= 0表示j和i之间不是回文串

dp[j][j] = (s[i]==s[j]&&(i-j<2||dp[j+1][i-1]))

string longestPalindrome(string s)
{
int l=0,r=0,max_length = 1;
int dp[1000][1000];
memset(dp,0,sizeof(dp));
int len = s.length();
for(int i=0;i<len;i++)  dp[i][i] = 1;
for(int i=0;i<len;i++)
for(int j=0;j<i;j++)
{
if(i-1>=0) dp[j][i] = (s[i]==s[j]&&(i-j<2||dp[j+1][i-1]));
if(dp[j][i]&&i-j+1>max_length)
{
l = j;
r = i;
max_length = i-j+1;
}
}
string ans;
for(int i=l;i<=r;i++)  ans.push_back(s[i]);
return ans;
}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120