-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path5.cpp
53 lines (44 loc) · 1.09 KB
/
5.cpp
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
class Solution {
public:
string longestPalindrome(string s) {
int n=s.length();
if(n<2){
return s;
}
int dp[n][n];
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
dp[i][j]=0;
}
}
for(int i=0;i<n;i++){
dp[i][i]=1;
}
int start=0;
int max_len=1;
for(int i=0;i<n-1;i++){
if(s[i]==s[i+1]){
dp[i][i+1]=1;
start=i;
max_len=2;
}
}
for(int len=3;len<=n;len++){
for(int i=0;i+len<=n;i++){
int j=i+len-1;
if(s[i]==s[j] && dp[i+1][j-1]){
dp[i][j]=1;
if(len>max_len){
start=i;
max_len=len;
}
}
}
}
string str;
for(int i=start;i<max_len+start;i++){
str=str+s[i];
}
return str;
}
};