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


题意:求两个数列的最长公共上升子序列?并输出方案。

设第1个数列为a,第2个数列为b。

题解:经典dp题目,用dp[i][j]表示取a数列的i个数,b数列的前j个数且第j个数必选的最长公共上升子序列。

转移就是:

如果a[i]==b[j]:

dp[i][j]=dp[i-1][k]+1,b[k]<b[j],k<j

否则:

dp[i][j]=dp[i-1][j] 

转移可以优化到O(1) 。

在转移的过程中记录路径,即可输出方案?

代码如下:

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
#include<string>
#include<stack>
#include<math.h>
#include<vector>
#define nn 550
#define inff 0x3fffffff
#define eps 1e-8
#define mod 1000000007
typedef long long LL;
const LL inf64=LL(inff)*inff;
using namespace std;
int n,m;
int a[nn],b[nn];
int dp[nn][nn];
int lj[nn][nn];
vector<int>sc;
int main()
{
    int t,i,j;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
        }
        scanf("%d",&m);
        for(i=1;i<=m;i++)
        {
            scanf("%d",&b[i]);
        }
        memset(dp,0,sizeof(dp));
        int ix;
        for(i=1;i<=n;i++)
        {
            ix=0;
            for(j=1;j<=m;j++)
            {
                if(a[i]==b[j])
                {
                    dp[i][j]=dp[i-1][ix]+1;
                    lj[i][j]=ix;
                }
                else
                {
                    dp[i][j]=dp[i-1][j];
                    lj[i][j]=j;
                }
                if(b[j]<a[i]&&dp[i-1][j]>dp[i-1][ix])
                    ix=j;
            }
        }
        int ans=0;
        for(i=1;i<=m;i++)
        {
            if(dp[n][i]>ans)
            {
                ans=dp[n][i];
                ix=i;
            }
        }
        printf("%d\n",ans);
        if(ans==0)
        {
            puts("");
        }
        else
        {
            sc.clear();
            int fc=n;
            while(1)
            {
                if(fc==0)
                    break;
                if(a[fc]==b[ix])
                    sc.push_back(a[fc]);
                ix=lj[fc][ix];
                fc--;
            }
            for(i=ans-1;i>=0;i--)
            {
                printf("%d%c",sc[i],i==0?'\n':' ');
            }
        }
        if(t)
            puts("");
    }
    return 0;
}


你可能感兴趣的:(dp,ACM)