-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLCIS.cpp
More file actions
64 lines (59 loc) · 1.13 KB
/
LCIS.cpp
File metadata and controls
64 lines (59 loc) · 1.13 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
#include <cstdio>
#include <cstring>
#include <stack>
#include <algorithm>
using namespace std;
const int MAX_N = 507;
int n, m;
int a[MAX_N], b[MAX_N];
int dp[MAX_N][MAX_N], path[MAX_N][MAX_N];
stack<int> s;
void print(int i, int j) {
if (i == 0 || j == 0)
return ;
if (~path[i][j]) {
print(i - 1, path[i][j]);
printf("%d ", b[j]);
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%d", &a[i]);
}
scanf("%d", &m);
for (int i = 1; i <= m; ++i) {
scanf("%d", &b[i]);
}
memset(path, -1, sizeof path);
int ans = 0, x = 0, y = 0;
for (int i = 1; i <= n; ++i) {
int idx = 0, Max = 0;
for (int j = 1; j <= m; ++j) {
dp[i][j] = dp[i - 1][j];
if (a[i] > b[j]) {
if (dp[i - 1][j] > Max) {
Max = dp[i - 1][j];
idx = j;
}
} else if (a[i] == b[j]) {
dp[i][j] = Max + 1;
path[i][j] = idx;
}
if (dp[i][j] > ans) {
ans = dp[i][j];
x = i, y = j;
}
}
}
printf("%d\n", ans);
int ans1 = ans;
while (ans1 > 0) {
if (~path[x][y]) s.push(b[y]), --ans1, y = path[x][y];
--x;
}
while (!s.empty())
printf("%d ", s.top()), s.pop();
puts("");
return 0;
}