ZOJ2432 Greatest Common Increasing Subsequence(最长公共上升子序列)

Greatest Common Increasing Subsequence
Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge
You are given two sequences of integer numbers. Write a program to determine their common increasing subsequence of maximal
possible length.
Sequence S1, S2, ..., SN of length N is called an increasing subsequence of a sequence A1, A2, ..., AM of length M if there exist 1 <= i1 < i2 < ...< iN <= M such that Sj = Aij for all 1 <= j <= N, and Sj < Sj+1 for all 1 <= j < N.

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

On the first line of the output print L - the length of the greatest common increasing subsequence of both sequences. On the second line print the subsequence itself. If there are several possible answers, output any of them.

This problem contains multiple test cases!

The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.

Sample Input

1

5
1 4 2 5 -12
4
-12 1 2 4

Sample Output

2
1 4
Source: Northeastern Europe 2003, Northern Subregion

#include "bits/stdc++.h"

using namespace std;
typedef long long ll;
const int mod = 1e9 + 7;
const int maxn = 1e5 + 100;
const int inf = 0x3f3f3f3f;

int pre[600][600];
int f[600][600];
int a[600], b[600];

int n, m;

void work(int x, int y, int z) {
    if (z == 0) return;
    while (a[x] != b[y]) x--;
    work(x, pre[x][y], z - 1);
    printf("%d ", b[y]);
}

void slove() {
    int out = 0;
    int pp;
    for (int i = 1; i <= n; i++) {
        int k = 0, maxx = 0;
        for (int j = 1; j <= m; j++) {
            if (a[i] == b[j]) {
                f[i][j] = f[i - 1][k] + 1;
                pre[i][j] = k;
            } else {
                f[i][j] = f[i - 1][j];
            }
            if (b[j] < a[i]) {
                if (f[i - 1][j] > maxx) {
                    maxx = f[i - 1][j];
                    k = j;
                }
            }
            if (f[i][j] > out) {
                out = f[i][j];
                pp = j;
            }
        }
    }
    printf("%d\n", out);
    work(n, pp, out);
}

void init() {
    memset(f, 0, sizeof(f));
}

int main() {
//    freopen("in.txt", "r", stdin);
    int _;
    scanf("%d", &_);
    while (_--) {
        init();
        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]);
        }
        slove();
        if (_) printf("\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值