HDU 1423 —— Greatest Common Increasing Subsequence

原题:http://acm.hdu.edu.cn/showproblem.php?pid=1423

题意:求最长上升公共子序列的个数;

注意:每组数据之间要空一行,最后一组数据结束不需要空行;


#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
const int maxn = 510;
int cas, n, m;
int dp[maxn][maxn];
int a[maxn], b[maxn];

int main()
{
	scanf("%d", &cas);
	while(cas--)
	{
		memset(dp, 0, sizeof dp);
		scanf("%d", &n);
		for(int i = 1;i<=n;i++)
			scanf("%d", &a[i]);
		scanf("%d", &m);
		for(int i = 1;i<=m;i++)
			scanf("%d", &b[i]);
		for(int i = 1;i<=n;i++)
		{
			int tmp = 0;
			for(int j = 1;j<=m;j++)
			{
				dp[i][j] = dp[i-1][j];
				if(b[j] < a[i] && tmp < dp[i-1][j])
					tmp = dp[i-1][j];
				if(a[i] == b[j])
					dp[i][j] = tmp + 1;
			}
		}
		int ans = -1;
		for(int i = 1;i<=m;i++)
			ans = max(ans, dp[n][i]);
		printf("%d\n", ans);
		if(cas != 0)	printf("\n");
	}
	return 0;
}


你可能感兴趣的:(LCIS)