-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongestCommonSubsequence.cpp
More file actions
65 lines (65 loc) · 1.46 KB
/
Copy pathLongestCommonSubsequence.cpp
File metadata and controls
65 lines (65 loc) · 1.46 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
62
63
64
65
#include "bits/stdc++.h"
// #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll dynamic(ll i,ll j,vector<ll>& a,vector<ll>& b) {
if(i >= a.size() || j >= b.size()) {
return 0;
}
if(a[i]==b[j]){
cout << a[i] << " ";
return 1+dynamic(i+1,j+1,a,b);
}
else return max(dynamic(i+1,j,a,b),dynamic(i,j+1,a,b));
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
int n,m;
cin >> n >> m;
vector<ll> a(n), b(m);
for(int i=0;i<n;i++){
cin >> a[i];
}
for(int i=0;i<m;i++){
cin >> b[i];
}
vector<vector<ll>> dp(m+1,vector<ll>(n+1,0));
for(int i=1;i<=m;i++){
for(int j=1;j<=n;j++){
if(b[i-1]==a[j-1]){
dp[i][j]=1+dp[i-1][j-1];
}
else{
dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
}
}
}
vector<ll> ans;
int i = m, j = n;
while (i > 0 && j > 0) {
if (b[i - 1] == a[j - 1]) {
ans.push_back(a[j - 1]);
i--;
j--;
}
else if (dp[i - 1][j] > dp[i][j - 1]) {
i--;
}
else {
j--;
}
}
cout << dp[m][n] << endl;
// for(int i=0;i<=n;i++){
// for(int j=0;j<=m;j++){
// cout << dp[i][j] << " ";
// }
// cout << endl;
// }
for(int i=ans.size()-1;i>=0;i--){
cout << ans[i] << " ";
}
cout << endl;
return 0;
}