-
Notifications
You must be signed in to change notification settings - Fork 0
/
F_lcs.cpp
67 lines (63 loc) · 1.34 KB
/
F_lcs.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<bits/stdc++.h>
using namespace std;
int dp[3005][3005]; //globally declare so that we can use this in any where
int topdownlcs(string s1,string s2,int n ,int m)
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(s1[i-1]==s2[j-1])
dp[i][j]=dp[i-1][j-1]+1;
else
{
int v1= dp[i-1][j];
int v2= dp[i][j-1];
dp[i][j]=max(v1,v2);
}
}
}
return dp[n][m];
}
void printlcs(string s1,string s2,int n,int m)
{
string ans;
int i=n,j=m;
while(i>0 && j>0)
{
if(s1[i-1]==s2[j-1])
{
ans.push_back(s1[i-1]);
i--;
j--;
}
else
{
if(dp[i-1][j]>dp[i][j-1])
{
i--;
}
else
{
j--;
}
}
}
reverse(ans.begin(),ans.end()); //for reversing the result
cout<<ans<<endl;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
string s1,s2;
cin>>s1>>s2;
int n=s1.length();
int m=s2.length();
memset(dp,0,sizeof(dp)); // for implement topdown approach
topdownlcs(s1,s2,n,m);
printlcs(s1,s2,n,m);
return 0;
}