【HDOJ】1423 Greatest Common Increasing Subsequence

LCIS

 1 /* 1423 */
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 
 6 #define MAXN 505
 7 
 8 int a[MAXN];
 9 int b[MAXN];
10 int c[MAXN];
11 int n, m;
12 int ans;
13 
14 int max(int a, int b) {
15     return a>b ? a:b;
16 }
17 
18 void solve() {
19     int i, j, k;
20     
21     memset(c, 0, sizeof(c));
22     for (i=0; i<n; ++i) {
23         k = 0;
24         for (j=0; j<m; ++j) {
25             if (a[i] == b[j]) {
26                 c[j] = max(c[j], k+1);
27             }
28             if (a[i] > b[j]) {
29                 k = max(k, c[j]);
30             }
31         }
32     }
33     ans = -1;
34     for (i=0; i<m; ++i)
35         ans = max(ans, c[i]);
36 }
37 
38 int main() {
39     int t;
40     int i, j, k;
41     
42     #ifndef ONLINE_JUDGE
43         freopen("data.in", "r", stdin);
44     #endif
45     
46     scanf("%d", &t);
47     while (t--) {
48         scanf("%d", &n);
49         for (i=0; i<n; ++i)
50             scanf("%d", &a[i]);
51         scanf("%d", &m);
52         for (i=0; i<m; ++i)
53             scanf("%d", &b[i]);
54         solve();
55         printf("%d\n", ans);
56         if (t)
57             printf("\n");
58     }
59     
60     return 0;
61 }

 

posted on 2015-01-16 12:30  Bombe  阅读(126)  评论(0编辑  收藏  举报

导航