Greatest Common Increasing Subsequence
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<cstdio>
#include<cstring>
using namespace std;
const int maxn=555;
int a[maxn],b[maxn],len[maxn];
int n,m;//a,b数组的长度
int LCIS()
{
int locate;
memset(len,0,sizeof(len));
for(int i=1;i<=n;++i)
{
locate=1;
for(int j=1;j<=m;++j)
{
if(b[j]<a[i]&&len[j]>len[locate])
locate=j;
if(a[i]==b[j])
len[j]=len[locate]+1;
}
}
int max=-1;
for(int i=1;i<=m;i++)
max=max>len[i]?max:len[i];
return max;
}
int main()
{
int t;
scanf("%d",&t);
int i,j;
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]);
int ans=LCIS();
printf("%d\n",ans);
if(t)
printf("\n");
}
return 0;
}