您的位置:首页 > 产品设计 > UI/UE

HDU 1423--Greatest Common Increasing Subsequence【LCIS】

2015-04-25 11:26 351 查看

Greatest Common Increasing Subsequence

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 4847 Accepted Submission(s): 1547



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




LCIS模板题,还是不太理解。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;

const int maxn = 510;

struct node //记录路径
{
	int x, y;
};

node map[maxn][maxn];

int a[maxn], b[maxn], dp[maxn][maxn], ex, ey;

void print (int i, int j){
	if(!i || !j)
		return ;
	int x = map[i][j].x, y = map[i][j].y;
	print(x, y);
	if(dp[x][y] + 1 == dp[i][j]){
		printf("%d", b[j]);
		if(i != ex && j != ey)
			printf(" ");
	}
}

int main (){
	int t;
	scanf("%d", &t);
	while(t--){
		int na, nb, ans = 0;
		scanf("%d", &na);
		for(int i = 1; i <= na; ++i)
			scanf("%d", &a[i]);
		scanf("%d", &nb);
		for(int j = 1; j <= nb; ++j)
			scanf("%d", &b[j]);
		memset(dp, 0, sizeof(dp));
		memset(map, 0, sizeof(map));
		for(int i = 1; i <= na; ++i){
			int maxn = 0, x = 0, y = 0;
			for(int j = 1; j <=nb; ++j){
				if(a[i] == b[j]){
					dp[i][j] = maxn + 1;
					map[i][j].x = x;
					map[i][j].y = y;
					if(ans < dp[i][j])
						ans = dp[i][j], ex = i, ey = j;
				}
				else{
					dp[i][j] = dp[i - 1][j];
					map[i][j].x = i - 1;
					map[i][j].y = j;
					if(a[i] > b[j] && maxn < dp[i - 1][j])
						maxn = dp[i - 1][j], x = i, y = j;
				}
			}
		}
		printf("%d\n", ans);
		//print(ex, ey);
		if(t) 
			printf("\n");
	}
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: