-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathLeetCode#5.cc
More file actions
39 lines (31 loc) · 939 Bytes
/
LeetCode#5.cc
File metadata and controls
39 lines (31 loc) · 939 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include<string.h>
#include<string>
int dp[1002][1002];
class Solution {
private:
public:
string longestPalindrome(string s) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int N = s.length();
if(N==0) return "";
memset(dp,-1,sizeof(dp));
int left=0,right = 0;
for(int i=0;i<N;i++) dp[i][i]=1;
for(int i=0;i<N-1;i++)
if(s[i]==s[i+1]){
left=i;right=i+1;
dp[i][i+1]=2;
}
for(int k=3;k<=N;k++)
for(int i=0;i+k-1<N;i++)
if(s[i]==s[i+k-1]&&dp[i+1][i+k-2]!=-1){
dp[i][i+k-1]=k;
if(right-left+1 < k){
left = i;
right = i+k-1;
}
}
return s.substr(left,right-left+1);
}
};