POJ 2127 Greatest Common Increasing Subsequence -- 动态规划
题目地址:http://poj.org/problem?id=2127
Description
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
Output
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 -- 动态规划的更多相关文章
- POJ 2127 Greatest Common Increasing Subsequence
You are given two sequences of integer numbers. Write a program to determine their common increasing ...
- 最长公共上升子序列 (poj 2127) (Greatest Common Increasing Subsequence)
\(Greatest Common Increasing Subsequence\) 大致题意:给出两个长度不一定相等的数列,求其中最长的公共的且单调递增的子序列(需要具体方案) \(solution ...
- 【简单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 题目:给你两个数字序列,求出这两个序列的最长公共上升子序列.输出最长的长度,并打表 ...
- HDOJ 1423 Greatest Common Increasing Subsequence -- 动态规划
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1423 Problem Description This is a problem from ZOJ 2 ...
- 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 ...
- LCIS POJ 2172 Greatest Common Increasing Subsequence
题目传送门 题意:LCIS(Longest Common Increasing Subsequence) 最长公共上升子序列 分析:a[i] != b[j]: dp[i][j] = dp[i-1][j ...
- HDU 1423 Greatest Common Increasing Subsequence ——动态规划
好久以前的坑了. 最长公共上升子序列. 没什么好说的,自己太菜了 #include <map> #include <cmath> #include <queue> ...
- HDU 1423 Greatest Common Increasing Subsequence(最长公共上升LCIS)
HDU 1423 Greatest Common Increasing Subsequence(最长公共上升LCIS) http://acm.hdu.edu.cn/showproblem.php?pi ...
- HDUOJ ---1423 Greatest Common Increasing Subsequence(LCS)
Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536 ...
随机推荐
- Runtime 10种用法
来源:haojingxue_iOS 链接:http://www.jianshu.com/p/3182646001d1 阅读了多篇运行时的文章,感觉都很不错,从几篇文章里面提取一些个人认为比较重要的,偏 ...
- xcopy拷贝判断是否成功 robocopy排除子目录
xcopy \\172.16.22.65\server\*.* C:\Inetpub\wwwroot\Server /h /r /s /yif %errorlevel% neq 0 echo copy ...
- C#-禁止调整窗体的大小
要是想禁止调整窗体的大小,可以查看:FormBorderStyle属性,该属性的设置中有一个"FixedSingle"的选择项,它可以禁止调整窗体的大小.
- Sublime Text 超好用的侧栏插件SideBarEnhancements
SideBarEnhancements插件有效地改进了Sublime Text的侧边栏.安装插件后在侧边栏上点击右键,可以找到一下新功能:在资源管理器中打开.新建文件.新建文件夹.以…打开.在浏览器中 ...
- Samba Linux和Windows互访
之前一直用的sshfs manager,但是会出现造成电脑黑屏的情况.网上也没找到其它类似的软件. 后面打算用Samba 直接搜到一篇很强大的文章. 1. 下载及安装Samba 推荐用yum来安装, ...
- EasyMock 使用方法与原理剖析--转载
原文地址:http://www.ibm.com/developerworks/cn/opensource/os-cn-easymock/ Mock 方法是单元测试中常见的一种技术,它的主要作用是模拟一 ...
- 运用surfaceView与MediaPlayer实现播放视频的功能
该程序运用了surfaceView与MediaPlayer结合,实现播放视频,surfaceView详情请见 SurfaceView的使用 使用了第三方包Volly里面的方法StringQueue下载 ...
- php验证码制作
目标: 使用php生成验证码 成品: 逻辑代码: authcode.php <?php header("Content-type:image/png"); session_s ...
- MATLAB基础入门笔记
为了参加那个电工杯,豁出去啦,时间真的很短,但是得挑战呀..对于MATLAB编程,有一些了解,MATLAB(矩阵实验室的简称)是一种专业的计算机程序,用于工程科学的矩阵数学运算,说说它的开发环境. 任 ...
- javascript for in 循环时,会取到Array.prototype
/** *删除数组指定下标或指定对象 */ if(!Array.prototype.remove){ Array.prototype.remove = function(obj){ for(var i ...