POJ 2127 Greatest Common Increasing Subsequence

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

题意 

输出两个序列的最长公共上升子序列。

分析

初始想法:定义dp[i][j]为以a[i]和b[j]为结尾的LCIS,这样转移时得找ax<ai以及by<bj,需要n^2,加上转移的循环,总复杂度n^4,TLE。

正解:既然上述定义超时,那么我们尝试减少一维,即把dp[i][j]定义为a[1...i]和b[1...j]并以b[j]为结尾的LCIS。

当a[i]==b[j],由LCS的转移可知由dp[i-1][j-1],但由于我们定义的这个状态,转移应为dp[i][j]=max(dp[i][k]),k<j。

当a[i]!=b[j],dp[i][j]=dp[i-1][j],因为规定了以b[j]为结尾,所以此时不可以由dp[i][j-1]转移而来。

另外可以优化一下,因为j是从小到大枚举的,那么我们可以保存当前行最大的dp[i][k]且符合b[k]<a[i](为了某个a[i]==b[x]的转移服务),到需要转移时就可以直接使用了

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include <queue>
#include <vector>
#include<bitset>
#include<map>
#include<deque>
using namespace std;
typedef long long LL;
const int maxn = 1e4+5;
const int mod = 77200211+233;
typedef pair<int,int> pii;
#define X first
#define Y second
#define pb push_back
//#define mp make_pair
#define ms(a,b) memset(a,b,sizeof(a))
const int inf = 0x3f3f3f3f;
#define lson l,m,2*rt
#define rson m+1,r,2*rt+1
typedef long long ll;
#define N 100010

int a[550],b[550];
int dp[550][550],pos[550][550];

int main(){
#ifdef LOCAL
    freopen("in.txt","r",stdin);
#endif // LOCAL
    int t,n,m;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        for(int i=1;i<=n;i++) scanf("%d",&a[i]);
        scanf("%d",&m);
        for(int j=1;j<=m;j++) scanf("%d",&b[j]);
        ms(dp,0);

        int ans=-1,mx,ei=0,ej=0,mj;
        for(int i=1;i<=n;i++){
            mx=0;
            for(int j=1;j<=m;j++){
                dp[i][j]=dp[i-1][j];
                pos[i][j]=-1;
                if(b[j]<a[i]&&dp[i-1][j]>mx){
                    mx=dp[i-1][j];
                    mj=j;
                }else if(a[i]==b[j]){
                    dp[i][j]=mx+1;
                    pos[i][j]=mj;
                }
                if(ans<dp[i][j]){
                    ans=dp[i][j];
                    ei=i;
                    ej=j;
                }
            }
        }
        cout<<ans<<endl;
        int temp[505];
        int tmp=ans;
        while(ans){
            if(pos[ei][ej]!=-1){
                temp[ans--]=b[ej];
                ej=pos[ei][ej];
            }
            ei--;
        }
        for(int i=1;i<=tmp;i++){
            printf("%d%c",temp[i],i==tmp?'\n':' ');
        }
        if(t) puts("");
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/fht-litost/p/8858391.html