HDU1423:Greatest Common Increasing Subsequence

点击打开题目链接

Greatest Common Increasing Subsequence

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


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
 

Source
ACM暑期集训队练习赛(二)
 

Recommend
lcy
 


=====================================题目大意=====================================


如题。


=====================================算法分析=====================================


最长公共上升子序列裸题。


=======================================代码=======================================




#include<stdio.h>
#include<string.h>

const int MAXLEN=505;

int T,Seq1[MAXLEN],Len1,Seq2[MAXLEN],Len2;

int MAX(int A,int B)
{
    return A>B?A:B;
}

int LCIS()
{
    int F[MAXLEN];
    memset(F,0,sizeof(F));
    for(int i=1;i<=Len1;++i)
    {
        int max=0;
        for(int j=1;j<=Len2;++j)
        {
            if(Seq2[j]<Seq1[i])  { max=MAX(max,F[j]); }
            if(Seq1[i]==Seq2[j]) { F[j]=max+1; }
        }
    }
    int ans=-1;
    for(int i=1;i<=Len1;++i)
    {
        ans=MAX(ans,F[i]);
    }
    return ans;
}

int main()
{
    while(scanf("%d",&T)==1)
    {
        for(int cas=1;cas<=T;++cas)
        {
             scanf("%d",&Len1);
             for(int i=1;i<=Len1;++i)
             {
                 scanf("%d",&Seq1[i]);
             }
             scanf("%d",&Len2);
             for(int i=1;i<=Len2;++i)
             {
                 scanf("%d",&Seq2[i]);
             }
             printf("%d%s",LCIS(),(cas==T?"\n":"\n\n"));
        }
    }
    return 0;
}

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