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<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<algorithm>
#include<stack>
using namespace std;
const int maxn = 505;
int a[maxn], b[maxn], n, m, T, f[maxn][maxn], Max, ans;

int main()
{
	scanf("%d", &T);
	while (T--)
	{
		memset(f, 0, sizeof(f));
		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]);
		ans = 0;
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= m; j++)
			if (a[i] == b[j])
			{
				Max = 0;
				for (int k = 1; k < j; k++) 
					if (b[k] < b[j]) Max = max(Max, f[i - 1][k]);
				f[i][j] = Max + 1;
				ans = max(ans, Max + 1);
			}
			else f[i][j] = f[i - 1][j];
		printf("%d\n", ans);
		if (T) printf("\n");
	}
	return 0;
}

#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<algorithm>
#include<stack>
using namespace std;
const int maxn = 505;
int a[maxn], b[maxn], n, m, T, f[maxn][maxn], Max, ans;

int main()
{
	scanf("%d", &T);
	while (T--)
	{
		memset(f, 0, sizeof(f));
		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]);
		ans = 0;
		for (int i = 1; i <= n; i++)
		{
			Max = 0;
			for (int j = 1; j <= m; j++)
			if (a[i] == b[j])
			{
				f[i][j] = Max + 1;
				ans = max(ans, Max + 1);
			}
			else
			{
				if (a[i] > b[j]) Max = max(Max, f[i - 1][j]);
				f[i][j] = f[i - 1][j];
			}
		}
		printf("%d\n", ans);
		if (T) printf("\n");
	}
	return 0;
}

#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<algorithm>
#include<stack>
using namespace std;
const int maxn = 505;
int a[maxn], b[maxn], n, m, T, f[maxn], Max, ans;

int main()
{
	scanf("%d", &T);
	while (T--)
	{
		memset(f, 0, sizeof(f));
		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]);
		ans = 0;
		for (int i = 1; i <= n; i++)
		{
			Max = 0;
			for (int j = 1; j <= m; j++)
			if (a[i] == b[j])
			{
				f[j] = Max + 1;
				ans = max(ans, f[j]);
			}
			else
				if (a[i] > b[j]) Max = max(Max, f[j]);
		}
		printf("%d\n", ans);
		if (T) printf("\n");
	}
	return 0;
}

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