-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathLongestPalindromicSubstring(Iterative).cpp
64 lines (59 loc) · 1.13 KB
/
LongestPalindromicSubstring(Iterative).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
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define mp make_pair
#define pb push_back
#define mt make_tuple
#define LD long double
#define gc getchar_unlocked
#define pc putchar_unlocked
#define MOD 1000000007
#define MAXN 1000003
#define bitcount __builtin_popcount
#define INF 2000000000
#define EPS 1e-6
template<typename T>T absll(T X)
{
if(X<0)
return -1*X;
else
return X;
}
vector<vector<bool> > dp;
int LongestPalindromicSubsequence(string str)
{
dp.resize(str.length()+1,vector<bool>(str.length()+1,false));
int N=str.length();
int maxlength=-1;
for(int i=0;i<N;i++)
{
dp[i][i]=true;
maxlength=1;
}
for(int i=0;i<N-1;i++)
{
if(str[i]==str[i+1])
{
dp[i][i+1]=true;
maxlength=2;
}
}
for(int l=3;l<=N;l++)
{
for(int i=0;i<N-l+1;i++)
{
int j=i+l-1;
if(str[i]==str[j]&&dp[i+1][j-1]==true)
{
dp[i][j]=true;
maxlength=max(maxlength,l);
}
}
}
return maxlength;
}
int main()
{
string str="aaaaaaaaaaaaaabababbbbbbbbbbbbbbbbbbbbbbbaaaaaaaaaaaaaaawwwwwwwwwwwwabbbaaxxxxxxxxxyyyyyyyyxxxxxxxxxxxx";
cout<<LongestPalindromicSubsequence(str)<<endl;
}