Greatest Common Increasing Subsequence_最长公共上升子序列

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6811    Accepted Submission(s): 2210


Problem Description
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
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
const int N=205;
int a[N],b[N],dp[N];
int main()
{
    int t,n,m;
    cin>>t;
    while(t--)
    {
        cin>>n;
        for(int i = 1; i<=n; i++)
            scanf("%d",&a[i]);
        cin>>m;
        for(int i = 1; i<=m; i++)
            scanf("%d",&b[i]);
          int ans=0;
           memset(dp,0,sizeof(dp));
           for(int i=1;i<=n;i++)
           {
               int k=0;
               for(int j=1;j<=m;j++)
               {
                   if(a[i]==b[j])
                   {
                           dp[j]=max(dp[j],dp[k]+1);

                   }
                   else if(a[i]>b[j]&&dp[k]<dp[j])
                       k=j;
                   if(ans<dp[j])
                       ans=dp[j];
               }
           }

        cout<<ans<<endl;
        if(t) cout<<endl;

    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/iwantstrong/p/5743781.html