-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLIS.cpp
More file actions
74 lines (69 loc) · 1.15 KB
/
Copy pathLIS.cpp
File metadata and controls
74 lines (69 loc) · 1.15 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
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <vector>
#include <fstream>
#include <algorithm>
#include <math.h>
#include <map>
using namespace std;
vector<vector<int>> dp;
vector<vector<int>> p;
inline int get(int i, int j) {
if (i < 0 || j < 0) {
return 0;
}
return dp[i][j];
}
int main() {
int n, m;
cin >> n;
vector<int> a(n);
for (int& i : a) {
cin >> i;
}
cin >> m;
vector<int> b(m);
for (int& i : b) {
cin >> i;
}
dp.resize(n, vector<int>(m, 0));
p.resize(n, vector<int>(m));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (a[i] == b[j]) {
dp[i][j] = get(i - 1, j - 1) + 1;
p[i][j] = 1;
}
else {
if (get(i - 1, j) > get(i, j - 1)) {
dp[i][j] = get(i - 1, j);
p[i][j] = 2;
}
else {
dp[i][j] = get(i, j - 1);
p[i][j] = 3;
}
}
}
}
int cur_i = n - 1;
int cur_j = m - 1;
vector<int> ans;
while (get(cur_i, cur_j)) {
if (p[cur_i][cur_j] == 1) {
ans.push_back(a[cur_i]);
--cur_i;
--cur_j;
}
else if (p[cur_i][cur_j] == 2) {
--cur_i;
}
else {
--cur_j;
}
}
cout << dp[n - 1][m - 1];
reverse(ans.begin(), ans.end());
for (int& i : ans) {
cout << i << ' ';
}
}