-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongestCommonSubset.cpp
More file actions
61 lines (59 loc) · 1.26 KB
/
LongestCommonSubset.cpp
File metadata and controls
61 lines (59 loc) · 1.26 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
52
53
54
55
56
57
58
59
60
61
#include<bits/stdc++.h>
using namespace std;
int lcsDP(int ***dp,string s1,int l,int r,int flag)
{
if(l>r)
{
return 0;
}
if(dp[l][r][flag]!=-1)
{
return dp[l][r][flag];
}
int ans=0;
if(flag==1)
{
if(s1[l]==s1[r])
{
ans=max(ans,1+lcsDP(dp,s1,l+1,r-1,1));
}
}
else
{
if(s1[l]==s1[r])
{
ans=max(ans,1+lcsDP(dp,s1,l+1,r-1,1));
}
ans=max(ans,max(lcsDP(dp,s1,l+1,r,0),lcsDP(dp,s1,l,r-1,0)));
}
return dp[l][r][flag]=ans;
}
int main()
{
string str;
cout<<"Enter the value of string:\n";
cin>>str;
string revstr=str;
int x=str.length();
reverse(revstr.begin(),revstr.end());
int ***dp=new int**[x+1];
for(int i=0;i<=x;i++)
{
dp[i]=new int*[x+1];
for(int j=0;j<=x;j++)
{
dp[i][j]=new int[2];
}
}
for(int i=0;i<=x;i++)
{
for(int j=0;j<=x;j++)
{
for(int k=0;k<2;k++)
{
dp[i][j][k]=-1;
}
}
}
cout<<"Length of Longest Common Subset in string "<<str<<" and "<<revstr<<" is : "<<lcsDP(dp,str,0,x-1,0)<<endl;
}