-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathLongestPalindromicSubsequence(Memoised).cpp
63 lines (55 loc) · 1.17 KB
/
LongestPalindromicSubsequence(Memoised).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
#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<int> > dp;
int LongestPalindromicSubsequence(string str,int st,int en)
{
if(dp[st][en]!=-1)
{
return dp[st][en];
}
if(st==en)
{
dp[st][en]=1;
return dp[st][en];
}
if(str[st]==str[en]&&en==st+1)
{
dp[st][en]=2;
return 2;
}
if(str[st]==str[en])
{
return dp[st][en]=(1+LongestPalindromicSubsequence(str,st+1,en-1));
}
else
{
return dp[st][en]=(max(LongestPalindromicSubsequence(str,st+1,en),LongestPalindromicSubsequence(str,st,en-1)));
}
}
int main()
{
string str="aaaaaaaaaaaaaabababbbbbbbbbbbbbbbbbbbbbbbaaaaaaaaaaaaaaawwwwwwwwwwwwabbbaaxxxxxxxxxyyyyyyyyxxxxxxxxxxxx";
int st=0;
int en=str.length()-1;
dp.resize(str.length(),vector<int>(str.length(),-1));
cout<<LongestPalindromicSubsequence(str,st,en)<<endl;
}