-
Notifications
You must be signed in to change notification settings - Fork 5
/
code(F - LCS).cpp
64 lines (55 loc) · 1.15 KB
/
code(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
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define endl '\n'
#define pb push_back
#define mp make_pair
#define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
using namespace std;
const ll v = 3001;
ll dp[v][v];
ll length(string& s,string& t,ll i,ll j)
{
// memset(dp,-1,sizeof(dp));
//Base case
if(i>=s.size() || j>=t.size())
return 0;
//Lookup
if(dp[i][j] != -1)
return dp[i][j];
//Rec case
if(s[i] == t[j])
return dp[i][j] = 1 + length(s,t,i+1,j+1);
else
return dp[i][j] = max(length(s,t,i+1,j), length(s,t,i,j+1));
}
int main()
{
fast_io;
string s,t;
cin>>s>>t;
memset(dp,-1,sizeof(dp));
//cout<<length(s,t,0,0)<<endl;
ll len = length(s,t,0,0);
ll i=0,j=0;
string ans = " ";
while(len)
{
if(s[i] == t[j])
{
ans.pb(s[i]);
i++;
j++;
len--;
}
else
{
if(dp[i][j+1] > dp[i+1][j])
j++;
else
i++;
}
}
cout<<ans<<endl;
return 0;
}