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

HDU 1423 Greatest Common Increasing Subsequence——dp

2018-03-26 11:02 369 查看
zoj2432的简化版,但是这道题忘了说输出结果两两之间一个空格,导致pe了一发
顺便用了滚动数组优化一下#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 510;
const int INF = 0x3f3f3f3f;
int T, n1, n2, a[maxn], b[maxn], dp[2][maxn];
int main() {
scanf("%d", &T);
for (int kase = 1; kase <= T; kase++) {
scanf("%d", &n1);
for (int i = 1; i <= n1; i++) scanf("%d", &a[i]);
scanf("%d", &n2);
for (int i = 1; i <= n2; i++) scanf("%d", &b[i]);
memset(dp, 0, sizeof(dp));
for (int i = 1; i <= n1; i++) {
int maxv = 0, p = 0;
for (int j = 1; j <= n2; j++) {
if (a[i] == b[j]) dp[i&1][j] = dp[!(i&1)][p] + 1;
else {
dp[i&1][j] = dp[!(i&1)][j];
if (a[i] > b[j] && maxv < dp[!(i&1)][j]) {
maxv = dp[!(i&1)][j]; p = j;
}
}
}
}
if (kase != 1) printf("\n");
int ans = 0;
for (int i = 1; i <= n2; i++) ans = max(ans, dp[n1&1][i]);
printf("%d\n", ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: