[dp](不连续)最大公共上升子序列 POJ 2127

来源:互联网 发布:mac上有没有hgame 编辑:程序博客网 时间:2024/04/28 14:48

Greatest Common Increasing Subsequence
Time Limit: 10000MS Memory Limit: 65536K
Total Submissions: 10159 Accepted: 2683
Case Time Limit: 2000MS Special Judge
Description

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 (-231 <= Ai < 231 ) — the sequence itself.
Output

On the first line of the output file 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.
Sample Input

5
1 4 2 5 -12
4
-12 1 2 4
Sample Output

2
1 4
Source

Northeastern Europe 2003, Northern Subregion

POJ 2127 Greatest Common Increasing Subsequence(最大上升公共子序列)
题意: 给两个数组,求最长公共递增子序列。
分析: dp[i,j]表示a串前i个字符,b串前j个字符组成的,并且以b[j]为结尾的最长的LCIS,
转移方程:
(1)dp[i,j]=dp[i-1,j]; //a[i]与b[j]不等,则以b[j]结尾的LCIS没有变,长度等于前一个以b[j]结尾的LCIS的长度。
(2)dp[i,j]=max(dp[i-1,k]+1,dp[i-1,k]); (1<=k<=j-1) //a[i]与b[j] 相等
以上转移方程是O(n^3)时间复杂度

优化:
由于最外层循环是 i,第二层是 j,循环 j 的时候,实际上同时找出dp[i-1,k] 的最大值mx。
方法:循环 j 的同时,若a[i]>b[j],更新mx(因为当且仅当a[i]>b[j]时(上升),后边循环 j 时才可能用到这个决策来转移)。若a[i] == b[j] 则用mx+1更新 dp[i,j],记录路径。
path[i][j]:记录a[i]匹配到前i个,b匹配到前j个,LCIS的倒数第二个的j值。

#include<stdio.h>#include<string.h>#define maxn 505#define clr(x)memset(x,0,sizeof(x))int a[maxn];int b[maxn];int dp[maxn][maxn];int path[maxn][maxn];int ans[maxn];int main(){    int l1,l2,i,j,ai,aj,mx,mj,res,tmp;    while(scanf("%d",&l1)!=EOF)    {        res=0;  clr(dp);  clr(path);        for(i=1;i<=l1;i++) scanf("%d",&a[i]);             scanf("%d",&l2);        for(i=1;i<=l2;i++) scanf("%d",&b[i]);        for(i=1;i<=l1;i++)        {            mx=0;            for(j=1;j<=l2;j++)            {                dp[i][j]=dp[i-1][j];                path[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;                    path[i][j]=mj;//记录最大上升子序列的倒数第二个数的j值;                }                if(res<dp[i][j])                {                    res=dp[i][j];                    ai=i;//记录最大上升子序列的路径最后一个数的坐标                    aj=j;//记录最大上升子序列的路径最后一个数的坐标                }            }        }        printf("%d\n",res);        tmp=res;        while(tmp)        {            if(path[a i ][ a j ]>-1)            {                ans[tmp--]=b[ a j ];                a j =path[ a i ][ a j ];            }            a I --;        }        for(i=1;i<=res;i++)  printf("%d%c",ans[i],i==res?'\n':' ');    }    return 0;}

欢迎留言,积极讨论,一起进步!

0 0
原创粉丝点击