poj2127Greatest Common Increasing Subsequence【LICS】
来源:互联网 发布:通达信公式源码 编辑:程序博客网 时间:2024/05/20 18:03
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
裸的LICS加上输出路径就把自己搞懵了;_; 一心想着记录父节点可以递归输出,然而远不用那么麻烦,最长递增公共子序列嘛==而且是二维的(废话),多加一个二维数组记录哈希了的前一个对应坐标,根据这个坐标记录每步的走法,这么说来,我原来递归的写法就只差一个数组呗,等回头补上
/*********poj21272016.2.121168K141MSC++1188B*********/#include <iostream>#include<cstdio>#include<cstring>using namespace std;int n,m,x[505],y[505],dp[505],pos[505][505],ans[505];void solve(){ memset(dp,0,sizeof(dp)); memset(pos,0,sizeof(pos)); for(int i=1;i<=n;i++) { int tmp=0; memcpy(pos[i],pos[i-1],sizeof(pos[0]));/// for(int j=1;j<=m;j++) { if(x[i]>y[j]&&dp[j]>dp[tmp]) tmp=j; if(x[i]==y[j]&&dp[tmp]+1>dp[j]) { dp[j]=dp[tmp]+1; pos[i][j]=i*(m+1)+tmp; } } } int maxn=0; for(int i=1;i<=m;i++) if(dp[maxn]<dp[i]) maxn=i; int i=m*n+n+maxn; for(int j=dp[maxn];j>0;j--) { ans[j]=y[i%(m+1)]; i=pos[i/(m+1)][i%(m+1)]; } printf("%d\n",dp[maxn]); for(i=1;i<=dp[maxn];i++) { if(i!=1) printf(" %d",ans[i]); else printf("%d",ans[i]); } puts("");}int main(){ // freopen("cin.txt","r",stdin); while(~scanf("%d",&n)) { for(int i=1;i<=n;i++) scanf("%d",&x[i]); scanf("%d",&m); for(int i=1;i<=m;i++) scanf("%d",&y[i]); solve(); } return 0;}
0 0
- poj2127Greatest 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)
- HDOJ 题目1423 Greatest Common Increasing Subsequence(LICS)
- poj 2127 Greatest Common Increasing Subsequence (记录路径LICS)
- poj2127 Greatest Common Increasing Subsequence(LICS+路径)
- hdu 1423Greatest Common Increasing Subsequence(lics)
- POJ 题目2127 Greatest Common Increasing Subsequence(LICS,输出路径)
- hdu 1423 Greatest Common Increasing Subsequence(最长公共递增子序列LICS)
- 【poj 2127】Greatest Common Increasing Subsequence 最长公共上升子序列lics+路径打印
- 【poj 2127】Greatest Common Increasing Subsequence lics(输出答案坑死)
- Greatest Common Increasing Subsequence
- zoj2432.Greatest+Common+Increasing+Subsequence
- HDU1423:Greatest Common Increasing Subsequence
- HDU1423 Greatest Common Increasing Subsequence
- hdu1423---Greatest Common Increasing Subsequence
- PAT1008数组元素循环右移
- 万能的Dialog
- PAT1031
- 用Docker运行Percona Server
- git使用入门篇
- poj2127Greatest Common Increasing Subsequence【LICS】
- (一)利用processing创建基本图形
- Cocos2dx:cocos2d-x-3.2版本学习过程中所遇到的一些问题(每一次都是一次作死的感觉)
- Ubuntu下10个厉害的Indicator小程序
- Android无序广播案例
- 守望者的逃离
- 强悍书单:概率与测度论+数理统计+随机过程+金融
- HTML5のWeb SQL
- 关系型数据库是如何运作的