最大公共子序列;
要读懂题目中对于输入的描述;
仍然是上次的GDKOI最大公共子串的写法。
1 # include <stdio.h>
2
3 int n;
4 int x[2];
5 int cor[21];
6 int cur[21];
7 int ans[441];
8
9 int get(int *x);
10
11 int main()
12 {
13 int i, t;
14
15 scanf("%d", &n);
16 for (i = 1; i <= n; ++i)
17 {
18 scanf("%d", &t);
19 cor[t] = i;
20 }
21
22 while (~scanf("%d", &t))
23 {
24 cur[t] = 1;
25 for (i = 2; i <= n; ++i)
26 {
27 scanf("%d", &t);
28 cur[t] = i;
29 }
30
31 x[0] = x[1] = n;
32 memset(ans, 0xff, sizeof(ans));
33 printf("%d\n", get(x));
34 }
35
36 return 0;
37 }
38
39 int get(int *x)
40 {
41 int index, ret, rem;
42 if (!x[0] || !x[1]) return 0;
43 index = x[0]-1 + (x[1]-1)*n;
44 if (ans[index] >= 0) return ans[index];
45 if (cor[x[0]] == cur[x[1]])
46 {
47 --x[0]; --x[1];
48 ret = get(x) + 1;
49 ++x[0]; ++x[1];
50 }
51 else
52 {
53 ret = 0;
54 --x[0]; if ((rem = get(x)) > ret) ret = rem; ++x[0];
55 --x[1]; if ((rem = get(x)) > ret) ret = rem; ++x[1];
56 }
57 ans[index] = ret;
58 return ret;
59 }