题目地址:http://poj.org/problem?id=2127

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

状态dp[i][j]表示seq1[i]从1到i与seq2[j]从1到j并以j为结尾的LCIS的长度

状态转移方程:

dp[i][j] = max(dp[i][k]) + 1, if seq1[i] ==seq2[j], 1 <= k  < j

dp[i][j] = dp[i-1][j], if seq1[i] != seq2[j]

#include <stdio.h>
#include <string.h> #define MAX 501 typedef struct path{
int x, y;
}Pre; int seq1[MAX], seq2[MAX];
int len1, len2;
int dp[MAX][MAX]; //状态dp[i][j]记录seq1前i个与seq2前j个并以seq2[j]为结尾的LCIS的长度
Pre pre[MAX][MAX];//pre[i][j]记录前驱
int path[MAX];//根据pre[i][j]回溯可得到LCIS
int index; int LCIS(){
int i, j;
int max, tx, ty;
int id_x, id_y;
int tmpx, tmpy;
//给dp[i][j]、pre[i][j]置初值
memset(dp, 0, sizeof(dp));
memset(pre, 0, sizeof(pre));
for (i = 1; i <= len1; ++i){
max = 0;
tx = ty = 0;
for (j = 1; j <= len2; ++j){
//状态转移方程
dp[i][j] = dp[i-1][j];
pre[i][j].x = i - 1;
pre[i][j].y = j;
if (seq1[i] > seq2[j] && max < dp[i-1][j]){
max = dp[i-1][j];
tx = i - 1;
ty = j;
}
if (seq1[i] == seq2[j]){
dp[i][j] = max + 1;
pre[i][j].x = tx;
pre[i][j].y = ty;
}
}
}
//找到LCIS最后的数字的位置
max = -1;
for (i = 1; i <= len2; ++i){
if (dp[len1][i] > max){
max = dp[len1][i];
id_y = i;
}
}
id_x = len1;
index = 0;
while (dp[id_x][id_y] != 0){
tmpx = pre[id_x][id_y].x;
tmpy = pre[id_x][id_y].y;
//若找到前一对公共点,则添加进路径
if (dp[tmpx][tmpy] != dp[id_x][id_y]){
path[index] = seq2[id_y];
++index;
}
id_x = tmpx;
id_y = tmpy;
}
return max;
} int main(void){
int i;
while (scanf("%d", &len1) != EOF){
for (i = 1; i <= len1; ++i)
scanf("%d", &seq1[i]);
scanf("%d", &len2);
for (i = 1; i <= len2; ++i)
scanf("%d", &seq2[i]); printf("%d\n", LCIS());
--index;
if (index >= 0)
printf("%d", path[index]);
for (i = index - 1; i >= 0; --i){
printf(" %d", path[i]);
}
printf("\n");
} return 0;
}

POJ 2127 Greatest Common Increasing Subsequence -- 动态规划的更多相关文章

  1. POJ 2127 Greatest Common Increasing Subsequence

    You are given two sequences of integer numbers. Write a program to determine their common increasing ...

  2. 最长公共上升子序列 (poj 2127) (Greatest Common Increasing Subsequence)

    \(Greatest Common Increasing Subsequence\) 大致题意:给出两个长度不一定相等的数列,求其中最长的公共的且单调递增的子序列(需要具体方案) \(solution ...

  3. 【简单dp】poj 2127 Greatest Common Increasing Subsequence【最长公共上升子序列】【模板】

    Sample Input 5 1 4 2 5 -12 4 -12 1 2 4 Sample Output 2 1 4 题目:给你两个数字序列,求出这两个序列的最长公共上升子序列.输出最长的长度,并打表 ...

  4. HDOJ 1423 Greatest Common Increasing Subsequence -- 动态规划

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1423 Problem Description This is a problem from ZOJ 2 ...

  5. POJ 1423 Greatest Common Increasing Subsequence【裸LCIS】

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1423 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  6. LCIS POJ 2172 Greatest Common Increasing Subsequence

    题目传送门 题意:LCIS(Longest Common Increasing Subsequence) 最长公共上升子序列 分析:a[i] != b[j]: dp[i][j] = dp[i-1][j ...

  7. HDU 1423 Greatest Common Increasing Subsequence ——动态规划

    好久以前的坑了. 最长公共上升子序列. 没什么好说的,自己太菜了 #include <map> #include <cmath> #include <queue> ...

  8. HDU 1423 Greatest Common Increasing Subsequence(最长公共上升LCIS)

    HDU 1423 Greatest Common Increasing Subsequence(最长公共上升LCIS) http://acm.hdu.edu.cn/showproblem.php?pi ...

  9. HDUOJ ---1423 Greatest Common Increasing Subsequence(LCS)

    Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536 ...

随机推荐

  1. JAVA常用设计模式整理

    设计模式:一个程序员对设计模式的理解:“不懂”为什么要把很简单的东西搞得那么复杂.后来随着软件开发经验的增加才开始明白我所看到的“复杂”恰恰就是设计模式的精髓所在,我所理解的“简单”就是一把钥匙开一把 ...

  2. Session笔记

    一.Session机制 session机制採用的是在server端保持 HTTP 状态信息的方案 . server使用一种类似于散列表的结构(也可能就是使用散列表)来保存信息. 当程序须要为某个cli ...

  3. ListOrderedMap

    要有序能够用List,要便于查找能够用Map,那既要有序又便于查找呢? 近期我就遇到了这样一个问题.Java没有给我们提供现成的类.我们全然能够自己开发个类继承List和Map(Java原来就有不能够 ...

  4. go操作数据库 Go-SQL-Driver/MySQL 使用详解

    go操作mysql的驱动包很多,这里讲解当下比较流行的Go-SQL-Driver/MySQL1.下载安装 执行下面两个命令: 下载:go get github.com/Go-SQL-Driver/My ...

  5. 教你如何选择Android游戏引擎

    我们进行Android游戏开发时选择游戏引擎是必须的,但是该如何选择呢?哪个Android游戏引擎更加适合自己呢?本文就提供了三个游戏引擎的对比说明,阐述了它们各自的特点,为大家选择引擎提供了参照. ...

  6. DOS 命令 attrib:修改文件属性

    有时候系统中病毒之后会把自己的所有文件的属性改为隐藏只读格式,如果手动修改的话太麻烦并且浪费时间,不如用 Attrib 命令试试. 步骤: 1:开始->运行,在里面输入 cmd,打开命令行界面: ...

  7. Qt数据库(sqlite) — 总结

    #include <QtSql>QT += sql QSqlDatabase类实现了数据库连接的操作QSqlQuery类用来执行SQL语句QSqlRecord类 封装数据库所有记录 第一: ...

  8. 查看MAC系统JRE和JDK版本

    经常有些软件有JRE或者JDK的版本要求,但怎么查询MAC上现在的JRE或JDK的版本呢?很简单,用这两个命令 这个是查询JRE的版本: $ java -version java version &q ...

  9. 聊聊Iconfont

    一.前言 说起Iconfont,对大多数人来说可能不是什么新的技术了,现在好多大网站已经开始使用Iconfont.博主今天主要是简单说一下它的使用方法,聊聊使用它时可能遇到哪些坑,不熟悉的同学可以简要 ...

  10. C语言枚举

    应该是 各种语言都支持枚举的. 所以这个 之前在我们java案例里面就出现过的季节代码再次出现了. 枚举帮我们起到一个变量值,在某一范围内的限定,用来避免一些非法值的键入. 值得注意的是,这里面的枚举 ...