zoj 2432 && hdoj 1423 Greatest Common Increasing Subsequence 【打印LCIS】

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




题意:给定两个序列a[]和b[],让你求出LCIS的长度并打印出LCIS。


思路:设置dp[i][j]为a[]前i个元素和b[]前j个元素且以b[j]结尾的最优LCIS的长度。

根据LCS的求法设置状态转移

一、a[i] != b[j],dp[i][j] = dp[i-1][j]。

二、a[i] == b[j],dp[i][j] = max(dp[i-1][k]) +1 其中(1 <= k < j && a[i] > b[k])。我们的目的是求出以a[i](b[j])结尾的最优LCIS,所以在a[i] <= b[k]的时候k是不可取的


最后扫一遍a[]就可以了。

为了保证O(n^2)的时间复杂度,我们需要快速求出k的值,这一点在dp的过程中可以轻松求出。

打印LCIS偷懒用了stack。


AC代码:


#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#define INF 0x3f3f3f
#define eps 1e-8
#define MAXN (500+1)
#define MAXM (100000)
#define Ri(a) scanf("%d", &a)
#define Rl(a) scanf("%lld", &a)
#define Rf(a) scanf("%lf", &a)
#define Rs(a) scanf("%s", a)
#define Pi(a) printf("%d\n", (a))
#define Pf(a) printf("%.2lf\n", (a))
#define Pl(a) printf("%lld\n", (a))
#define Ps(a) printf("%s\n", (a))
#define W(a) while(a--)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define LL long long
#define lson o<<1, l, mid
#define rson o<<1|1, mid+1, r
#define ll o<<1
#define rr o<<1|1
using namespace std;
int dp[MAXN][MAXN];
int prea[MAXN][MAXN], preb[MAXN][MAXN];
int a[MAXN], b[MAXN];
void LCIS(int pa, int pb)
{
    stack<int> S; S.push(b[pb]);
    while(1)
    {
        int x = prea[pa][pb];
        int y = preb[pa][pb];
        if(dp[x][y] == 0)
            break;
        if(dp[pa][pb] == dp[x][y] + 1)
            S.push(b[y]);
        pa = x; pb = y;
    }
    int use = 0;
    while(!S.empty())
    {
        if(use) printf(" ");
        printf("%d", S.top()); S.pop();
        use++;
    }
    printf("\n");
}
int main()
{
    int t; Ri(t);
    W(t)
    {
        int n, m;
        Ri(n);
        for(int i = 1; i <= n; i++)
            Ri(a[i]);
        Ri(m);
        for(int i = 1; i <= m; i++)
            Ri(b[i]);
        CLR(dp, 0); CLR(prea, 0); CLR(preb, 0);
        for(int i = 1; i <= n; i++)
        {
            int temp = 0, k = 0;
            for(int j = 1; j <= m; j++)
            {
                if(a[i] != b[j])
                {
                    dp[i][j] = dp[i-1][j];
                    prea[i][j] = i-1;
                    preb[i][j] = j;
                }
                else
                {
                    dp[i][j] = dp[i-1][k] + 1;
                    prea[i][j] = i-1;
                    preb[i][j] = k;
                }
                if(a[i] > b[j] && temp < dp[i-1][j])
                {
                    temp = dp[i-1][j];
                    k = j;
                }
            }
        }
        int ans = 0, pos;
        for(int i = 1; i <= m; i++)
        {
            if(ans < dp[n][i])
            {
                ans = dp[n][i];
                pos = i;
            }
        }
        printf("%d\n", ans);
        if(ans)
            LCIS(n, pos);
        if(t)
            printf("\n");
    }
    return 0;
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值