HDU - 1423 Greatest Common Increasing Subsequence

This is a problem from ZOJ 2432.To make it easyer,you just need output the length of the subsequence.

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

output print L - the length of the greatest common increasing subsequence of both sequences.

Sample Input

1

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

Sample Output

2

题解:输入两个数字串a,b,求出它们最长公共递增子序列的长度。

代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int dp[550][550];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,m,a[550],b[550],i,j;
        memset(dp,0,sizeof(dp));
        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]);
        for(i=1; i<=n; i++)
        {
            int ans=0;
            for(j=1; j<=m; j++)
            {
                dp[i][j]=dp[i-1][j];
                if(a[i]>b[j]&&ans<dp[i-1][j])
                    ans=dp[i-1][j];
                if(a[i]==b[j])
                    dp[i][j]=ans+1;
            }
        }
        int sum=0;
        for(i=1; i<=m; i++)
            if(sum<dp[n][i])
                sum=dp[n][i];
        printf("%d\n",sum);
        if(t)
            printf("\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/GJLfly/article/details/81434656