POJ 2127 Greatest Common Increasing Subsequence (最长公共上升子序列+记录路径)
2013-04-10 21:01
591 查看
Greatest Common Increasing Subsequence
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
Sample Output
View Code
Time Limit: 10000MS | Memory Limit: 65536K | |
Total Submissions: 8021 | Accepted: 2138 | |
Case Time Limit: 2000MS | Special Judge |
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 思路:定义dp[i][j]表示第一个序列seq1[]到位置i,第二个序列seq2[]到位置j时的最长公共上升子序列长度。当seq1[i] != seq2[j]时,dp[i][j] = dp[i-1][j];否则更新dp[i][j]的值为之前最长的长度+1,同时用二维数组pre[i][j]存储上个最长长度是以seq2[]的哪个结尾的,假设之前的最长长度是dp[i'][j'],则pre[i][j] = j'。
View Code
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #define SIZE 505 6 7 using namespace std; 8 9 int dp[SIZE][SIZE]; 10 int pre[SIZE][SIZE]; 11 int path[SIZE]; 12 int seq1[SIZE],seq2[SIZE]; 13 int N,M; 14 15 int main() 16 { 17 while(~scanf("%d",&N)) 18 { 19 for(int i=1; i<=N; i++) 20 scanf("%d",&seq1[i]); 21 scanf("%d",&M); 22 for(int i=1; i<=M; i++) 23 scanf("%d",&seq2[i]); 24 int ans = -1; 25 int pos1,pos2; 26 memset(dp,0,sizeof(dp)); 27 memset(pre,0,sizeof(pre)); 28 memset(path,0,sizeof(path)); 29 for(int i=1; i<=N; i++) 30 { 31 int maxi = 0,temp = 0; 32 for(int j=1; j<=M; j++) 33 { 34 if(seq1[i] != seq2[j]) 35 { 36 dp[i][j] = dp[i-1][j]; 37 if(seq1[i] > seq2[j] && maxi < dp[i][j]) 38 { 39 maxi = dp[i][j]; 40 temp = j; 41 } 42 } 43 if(seq1[i] == seq2[j]) 44 { 45 dp[i][j] = maxi + 1; 46 pre[i][j] = temp; 47 } 48 if(dp[i][j] > ans) 49 { 50 ans = dp[i][j]; 51 pos1 = i; 52 pos2= j; 53 } 54 } 55 } 56 printf("%d\n",ans); 57 int len = ans; 58 if(ans > 0) 59 path[ans--] = pos2; 60 while(ans && pos1 && pos2) 61 { 62 if(pre[pos1][pos2] > 0) 63 { 64 path[ans--] = pre[pos1][pos2]; 65 pos2 = pre[pos1][pos2]; 66 } 67 pos1--; 68 } 69 for(int i=1; i<len; i++) 70 printf("%d ",seq2[path[i]]); 71 printf("%d\n",seq2[path[len]]); 72 } 73 return 0; 74 }
相关文章推荐
- 【poj 2127】Greatest Common Increasing Subsequence 最长公共上升子序列lics+路径打印
- poj 2127 Greatest Common Increasing Subsequence(最长公共上升子序列dp)
- 【简单dp】poj 2127 Greatest Common Increasing Subsequence【最长公共上升子序列】【模板】
- ZOJ 2432 Greatest Common Increasing Subsequence(最长公共上升子序列+路径打印)
- 最长单调递增公共子序列(路径记录+poj2127+zoj2432)Greatest Common Increasing Subsequence
- HDU1423&ZOJ2432 - Greatest Common Increasing Subsequence(LCIS最长公共上升子序列模板)
- zoj 2432 Greatest Common Increasing Subsequence(最长公共上升子序列)
- hdu 1423 Greatest Common Increasing Subsequence(DP 最长公共上升子序列)
- hdu 1423 Greatest Common Increasing Subsequence 最长公共上升子序列
- hdu 1423 Greatest Common Increasing Subsequence(DP 最长公共上升子序列)
- hdu 1423 Greatest Common Increasing Subsequence 最长公共上升子序列
- HDU 1423 Greatest Common Increasing Subsequence (最长公共上升子序列)【模板】
- hdu 1423 Greatest Common Increasing Subsequence(最长公共上升子序列dp)
- 最长公共上升子序列——hdu1423 Greatest Common Increasing Subsequence
- HDOJ Greatest Common Increasing Subsequence(LCIS最长公共上升子序列)
- POJ 2127 Greatest Common Increasing Subsequence【最长公共递增子序列】
- Greatest Common Increasing Subsequence-最长公共上升子序列
- hdu 1423 Greatest Common Increasing Subsequence 最长公共上升子序列
- HDU 1423 Greatest Common Increasing Subsequence(最长公共上升LCIS)
- HDOJ 1423 Greatest Common Increasing Subsequence 【DP】【最长公共上升子序列】