-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprint_lcs_snippet.cpp
62 lines (55 loc) · 1.21 KB
/
print_lcs_snippet.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
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define float double
#define pb push_back
#define mp make_pair
#define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
int f(string a, string b, int i, int j, vector<vector<int>>&dp)
{
if(i<0 or j<0)return 0;
if(dp[i][j]!=-1)return dp[i][j];
int len = 0;
if(a[i]==b[j]){
len = max(len, 1 + f(a, b, i-1, j-1, dp));
}else len = f(a, b, i-1, j, dp);
return dp[i][j] = len;
}
void solve()
{
string a, b;
cin>>a;
cin>>b;
vector<vector<int>>dp(a.size()+1, vector<int>(b.size()+1, -1));
int val = f(a, b, a.size()-1, b.size()-1, dp);
cout<<val<<endl;
string ans;
int i=a.size()-1, j=b.size()-1;
while(i>=0 and j>=0)
{
if(a[i]==b[j]){
ans+=a[i];
i--;
j--;
}else{
if(dp[i][j-1]>dp[i-1][j]){
j--;
}else if(dp[i][j-1]<dp[i-1][j]){
i--;
}
}
}
reverse(ans.begin(), ans.end());
cout<<ans<<endl;
}
int32_t main()
{
fast
int t = 1;
// cin >> t;
while (t--)
{
solve();
}
return 0;
}