POJ2127 Greatest Common Increasing Subsequence (LICS)
来源:互联网 发布:yankee 知乎 编辑:程序博客网 时间:2024/05/20 20:21
Greatest Common Increasing Subsequence
Time Limit: 10000MS Memory Limit: 65536KTotal Submissions: 11617 Accepted: 3040Case 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
思路:最长递增公共子序列题目,要输出最长子序列。
#include <iostream>#include <cstdio>#include <cmath>#include <cstring>#include <algorithm> using namespace std;int n,m,a[505],b[505],dp[505],mark[505][505],ans[505];int LICS(){int MAX,k;for(int i = 1; i <= n; i ++){k = 0;memcpy(mark[i],mark[i - 1],sizeof(mark[0]));for(int j = 1; j <= m; j ++){if(a[i - 1] > b[j - 1] && dp[k] < dp[j])k = j;if(a[i - 1] == b[j - 1] && dp[k] + 1 > dp[j]){dp[j] = dp[k] + 1;mark[i][j] = i * (m + 1) + k;}}}MAX = 0;for(int i = 1; i <= m; i ++)if(dp[i] > dp[MAX])MAX = i;int i = (m + 1) * n + MAX;for(int j = dp[MAX]; j > 0; j --){ans[j - 1] = b[i % (m + 1) - 1];i = mark[i/(m + 1)][i%(m + 1)];}return dp[MAX];}int main(){scanf("%d",&n);for(int i = 0; i < n; i ++)scanf("%d",&a[i]); scanf("%d",&m);for(int i = 0; i < m; i ++)scanf("%d",&b[i]);memset(dp,0,sizeof(dp));memset(ans,0,sizeof(ans));int k = LICS();printf("%d\n",k);for(int i = 0; i < k; i ++){if(i)printf(" ");printf("%d",ans[i]);}return 0;}
0 0
- POJ2127:Greatest Common Increasing Subsequence(LICS)
- POJ2127 Greatest Common Increasing Subsequence (LICS)
- poj2127 Greatest Common Increasing Subsequence(LICS+路径)
- poj2127 Greatest Common Increasing Subsequence
- HDU1423&POJ2127-Greatest Common Increasing Subsequence
- HDU1423:Greatest Common Increasing Subsequence(LICS)
- HDU1423 Greatest Common Increasing Subsequence (LICS)
- HDOJ 题目1423 Greatest Common Increasing Subsequence(LICS)
- poj 2127 Greatest Common Increasing Subsequence (记录路径LICS)
- hdu 1423Greatest Common Increasing Subsequence(lics)
- Greatest Common Increasing Subsequence
- poj2127Greatest Common Increasing Subsequence【LICS】
- 最长单调递增公共子序列(路径记录+poj2127+zoj2432)Greatest Common Increasing Subsequence
- 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(输出答案坑死)
- zoj2432.Greatest+Common+Increasing+Subsequence
- JAXB的使用(java对象和xml文本的相互转换)
- codeVS 1214 线段覆盖
- IOS学习之block代码块
- SSM框架整合配置大全
- Java的基本数据类型
- POJ2127 Greatest Common Increasing Subsequence (LICS)
- Servlet学习笔记
- Swift3.0从入门到放弃(二)
- Unit 2-Lecture9: Coloring & Connectivity
- 1.Object
- Unit 2-Lecture10: Forests & Tree
- IntentFilter匹配规则
- 阿里巴巴Java开发手册
- Ubuntu使用Checkpoint SNX踩坑记