HDU 1423 Greatest Common Increasing Subsequence

经典LIS问题,去最长上升公共子序(要注意输出格式)

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxm=505;
int a[maxm];
int b[maxm];
int dp[maxm][maxm];
int n,m;
int LIS()
{
    int i,j;
    int ans=-1;
    for(i=1; i<=n; i++)
    {
        int M=0;
        for(j=1; j<=m; j++)
        {
            if(M<dp[i-1][j]&&a[i]>b[j])
            {
                M=dp[i-1][j];
            }
            if(a[i]!=b[j])
            {
                dp[i][j]=dp[i-1][j];
            }
            else
            {
                dp[i][j]=M+1;
            }
            if(dp[i][j]>ans)
                ans=dp[i][j];
        }
    }
    return ans;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        memset(dp,0,sizeof(dp));
        scanf("%d",&n);
        int i,j;
        for(i=1; i<=n; i++)
        {
            scanf("%d",&a[i]);
        }
        scanf("%d",&m);
        for(i=1; i<=m; i++)
        {
            scanf("%d",&b[i]);
        }
        printf("%d\n",LIS());
        if(t)
            printf("\n");
    }
    return 0;
}


你可能感兴趣的:(HDU 1423 Greatest Common Increasing Subsequence)