POJ 题目2127 Greatest Common Increasing Subsequence(LICS,输出路径)
来源:互联网 发布:淘宝钱柜数码 编辑:程序博客网 时间:2024/05/27 06:56
Greatest Common Increasing Subsequence
Time Limit: 10000MS Memory Limit: 65536KTotal Submissions: 9762 Accepted: 2584Case 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 .
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
51 4 2 5 -124-12 1 2 4
Sample Output
21 4
Source
Northeastern Europe 2003, Northern Subregion
ac代码
#include<stdio.h>#include<string.h>int n,m;int dp[1010][1010],path[1010][1010],ans[1010],aj,ai,res,a[1010],b[1010];void LCS(){int i,j,mj;res=0;memset(dp,0,sizeof(dp));memset(path,-1,sizeof(path));for(i=1;i<=n;i++){int ma=0;for(j=1;j<=m;j++){dp[i][j]=dp[i-1][j];if(b[j]<a[i]&&dp[i][j]>ma){ma=dp[i][j];mj=j;}else{if(b[j]==a[i]){dp[i][j]=ma+1;path[i][j]=mj;}}if(res<dp[i][j]){res=dp[i][j];ai=i;aj=j;}}}}int main(){while(scanf("%d",&n)!=EOF){int i,j;for(i=1;i<=n;i++)scanf("%d",&a[i]);scanf("%d",&m);for(j=1;j<=m;j++)scanf("%d",&b[j]);LCS();printf("%d\n",res);int temp=res;while(temp){if(path[ai][aj]>-1){ans[temp--]=b[aj];aj=path[ai][aj];}ai--;}for(i=1;i<=res;i++)printf("%d%c",ans[i],i==res?'\n':' ');}}
0 0
- POJ 题目2127 Greatest Common Increasing Subsequence(LICS,输出路径)
- poj 2127 Greatest Common Increasing Subsequence (记录路径LICS)
- 【poj 2127】Greatest Common Increasing Subsequence lics(输出答案坑死)
- POJ 2127 Greatest Common Increasing Subsequence(LCIS+输出路径)
- HDOJ 题目1423 Greatest Common Increasing Subsequence(LICS)
- poj2127 Greatest Common Increasing Subsequence(LICS+路径)
- 【poj 2127】Greatest Common Increasing Subsequence 最长公共上升子序列lics+路径打印
- hdu 1423Greatest Common Increasing Subsequence(lics)
- HDU1423:Greatest Common Increasing Subsequence(LICS)
- POJ2127:Greatest Common Increasing Subsequence(LICS)
- HDU1423 Greatest Common Increasing Subsequence (LICS)
- POJ2127 Greatest Common Increasing Subsequence (LICS)
- POJ 2127 Greatest Common Increasing Subsequence
- poj 2127 Greatest Common Increasing Subsequence
- poj 2127 Greatest Common Increasing Subsequence
- Greatest Common Increasing Subsequence poj 2127
- poj 2127 Greatest Common Increasing Subsequence
- POJ 2127 Greatest Common Increasing Subsequence
- log4j 按时间、大小产生新的日志文件【转】
- 【瞎搞】 Codeforces 460D Little Victor and Set 位运算构造
- iOS —— QQ中未读气泡拖拽消失的实现
- 黑马程序员------c.全局变量
- 工作区和暂存区
- POJ 题目2127 Greatest Common Increasing Subsequence(LICS,输出路径)
- 泛型
- Android动画之Interpolator和AnimationSet
- MapReduce工作原理图文详解
- UIView详解
- 变长参数表:<stdarg.h>介绍及在iOS中应用
- 回收苯酐,顺酐,马来酸酐,琥珀酸酐13403306214原料回收
- 数据库索引
- 管理修改