forked from tanvi1004/competitivesolutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLCS.cpp
104 lines (91 loc) · 1.9 KB
/
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
///////// ///// //// ////
// // // // // // //
////// // // // // //
// // /// // // //
//////// // // // //
*/
#include<bits/stdc++.h>
#include<assert.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long ull;
#define Fast ios_base::sync_with_stdio(false); cin.tie(NULL);
#define fo(i,s,n) for(int i=s;i<n;i++)
#define pb(x) push_back(x);
#define mod 1000000007
int main()
{
Fast
string s,t;
cin>>s;
cin>>t;
ll ls=s.length();
ll lt=t.length();
ll dp[ls+1][lt+1];
//cout<<"ls"<<ls<<" lt"<<lt<<endl;
fo(i,0,ls+1)
dp[i][0]=0;
fo(i,0,lt+1)
dp[0][i]=0;
for(ll i=1;i<=ls;i++)
{
for(ll j=1;j<=lt;j++)
{
if(t[j-1]==s[i-1])
{
dp[i][j]=dp[i-1][j-1]+1;
}
else
{
dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
}
}
}
/* fo(i,0,ls+1)
{
fo(j,0,lt+1)
cout<<dp[i][j]<<" ";
cout<<endl;
}
//cout<<dp[ls][lt];
cout<<endl;*/
string res;
ll i=ls,j=lt;
while(i!=0&&j!=0)
{
if(s[i-1]==t[j-1])
{
res+=s[i-1]; //res is string
j--;
i--;
}
else if(dp[i-1][j]==dp[i][j])
i--;
else
j--;
}
reverse(res.begin(),res.end());
cout<<res<<endl;
/*
while(i>0&&j>0)
{
if(dp[i][j]==dp[i-1][j-1]+1)
{
ans.push(t[j-1]);
i--;
j--;
}
else
{
j--;
}
}
while(!ans.empty())
{
cout<<ans.top();
ans.pop();
}
cout<<endl;*/
return 0;
}