-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode_Q5_LongestPalindromicSubstring.cpp
More file actions
51 lines (45 loc) · 1.47 KB
/
LeetCode_Q5_LongestPalindromicSubstring.cpp
File metadata and controls
51 lines (45 loc) · 1.47 KB
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
#include <iostream>
using namespace std;
int main(){
string s = "cbbc";
int length = s.length();
if (length == 0){return 0;}
int dp[length][length] ={};
int longest = 0;
int index1=0,index2=0;
for (int i = 0; i < length; ++i){
for(int j = 0; j<length; ++j){
int k = i+j;
if(i == 0){
dp[j][k] = 1;
}else{
if(k>=length){break;}
if(s[j] == s[k]){
if(k==j+1){
dp[j][k] =2 + dp[j+1][k-1];
//cout <<"a:"<<j<<','<<k<<' '<< dp[j][k] << endl;
}
else if(max(dp[j][k-1],dp[j+1][k-1]) >= k-j-1){
dp[j][k] = 2 + dp[j+1][k-1];
//cout <<"b:"<<j<<','<<k<<' '<< dp[j][k] << endl;
}else{
dp[j][k] = dp[j][k-1];
//cout <<"c:"<< j<<','<<k<<' '<< dp[j][k] << endl;
}
}else{
dp[j][k] = dp[j][k-1];
}
}
if (dp[j][k] > longest){
longest = dp[j][k];
index1 = j;
index2 = k;
}
//cout <<j<<','<<k <<':'<<dp[j][k]<<endl;
}
}
for(int i=index1;i<index2 +1;i++){
cout << s[i];
}
return 0;
}