zoj 2432 Greatest Common Increasing Subsequence

Greatest Common Increasing Subsequence
Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge
You are given two sequences of integer numbers. Write a program to determine their common increasing subsequence of maximal
possible length.

Sequence S1, S2, ..., SN of length N is called an increasing subsequence of a sequence A1, A2, ..., AM of length M if there exist 1 <= i1 < i2 < ...< iN <= M such that Sj = Aij for all 1 <= j <= N, and Sj < Sj+1 for all 1 <= j < N.


Input

Each sequence is described with M - its length (1 <= M <= 500) and M integer numbers Ai (-2^31 <= Ai < 2^31) - the sequence itself.


Output

On the first line of the output print L - the length of the greatest common increasing subsequence of both sequences. On the second line print the subsequence itself. If there are several possible answers, output any of them.


This problem contains multiple test cases!

The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.


Sample Input

1

5
1 4 2 5 -12
4
-12 1 2 4


Sample Output

2
1 4

//C++    110ms    3172
//不知道为什么用一维会WA,于是参考别人用了二维
//有点坑,格式要求不严格 
#include<stdio.h>
#include<string.h>
struct node{
     int x,y;
}path[505][505];
int dp[505][505];
int a[505],b[505];
int main(void)
{
    int t,n,m;
    while(scanf("%d",&t)!=EOF)
    while(t--)
    {
        memset(dp,0,sizeof(dp));
        memset(path,0,sizeof(path));
        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]);
        for(int i=1;i<=n;i++){
            int tx=0,ty=0;
            int maxn=0;
            for(int j=1;j<=m;j++){
                dp[i][j]=dp[i-1][j];
                path[i][j].x=i-1;
                path[i][j].y=j;
                if(a[i]>b[j] && maxn<dp[i-1][j]){
                    maxn=dp[i-1][j];
                    tx=i-1;ty=j;
                }
                if(a[i]==b[j]){
                    dp[i][j]=maxn+1;
                    path[i][j].x=tx;
                    path[i][j].y=ty;
                }
            }
        }
        int id=0;
        int save[505];
        for(int i=1;i<=m;i++)
            if(dp[n][i]>dp[n][id]) id=i;
        printf("%d\n",dp[n][id]);
        int cnt=0,tx=n,ty=id;
        while(dp[tx][ty]!=0){
            int tx0=path[tx][ty].x;
            int ty0=path[tx][ty].y;
            if(dp[tx0][ty0]!=dp[tx][ty]){//排除重复元素 
                save[cnt++]=b[ty];
            }
            tx=tx0;
            ty=ty0;
        }
        for(int i=cnt-1;i>=0;i--) 
            printf(i>0?"%d ":"%d\n",save[i]);
        if(t) printf("\n"); //有没有好像都能过 
    }
    return 0;
}

 

posted @ 2013-10-02 08:59  heaventouch  阅读(136)  评论(0编辑  收藏  举报