HDU 1423 子序列

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;
}



 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章