-
Notifications
You must be signed in to change notification settings - Fork 0
/
longest_palindrome_substring.cpp
67 lines (51 loc) · 1.16 KB
/
longest_palindrome_substring.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include<iostream>
using namespace std;
void longestPall(std::string s, int& start, int &len){
bool **dp;
dp=new bool*[s.length()];
for(int i=0;i<s.length() ; i++){
dp[i]=new bool[s.length()];
}
for(int i=0;i<s.length() ; i++){
dp[i][i]=true;
}
for(int i=0;i<s.length()-1 ; i++){
if(s[i]==s[i+1])
dp[i][i+1]=true;
}
for(int cl=3;cl <= s.length() ; cl++){
for(int i=0; i<s.length()-cl+1 ; i++){
int j= i + cl -1 ;
if( dp[i+1][j-1] && (s[i] == s[j]))
dp[i][j]=true;
else
dp[i][j]=false;
}
}
int max=0;
for(int i=0;i<s.length() ; i++){
for(int j=0;j<s.length() ; j++){
// to display DP table Define -DDEBUG
#ifdef DEBUG
cout<<dp[i][j];
#endif
if(dp[i][j] && ( max <= (j-i)))
{
max=(j-i);
start=i;
len=j-i+1;
}
}
#ifdef DEBUG
cout<<endl;
#endif
}
}
int main(){
std::string s;
cin>>s;
int st,l;
longestPall(s,st,l);
cout<<s.substr(st,l)<<endl;
return 0;
}