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 ...
随机推荐
- JS基础DOM篇之一:何为DOM?
近日在园子看了一篇文章,一位前端负责人问应聘者何为DOM事件流的三个阶段,我当时一看也是懵圈,于是强迫症复发,遂想要搞清楚它.谁知在查资料的过程中发现有好多关于DOM的概念也是模糊不清,便决定继续延伸 ...
- linux tomcat自启动设置
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...
- 对get_baserel_parampathinfo函数的学习
/* * get_baserel_parampathinfo * Get the ParamPathInfo for a parameterized path for a base relation, ...
- tableview 上拉时 标题行出现在顶部不动效果
类似这种效果: 其实很简单,利用tableview 的plain属性,然后使用section,其实滑上去不动的是 section的headView. -(NSInteger)numberOfSect ...
- 【转】linux中的sed命令
转自:http://www.cnblogs.com/shineshqw/articles/1978122.html 功能说明: 利用script来处理文本文件. 语 法:sed [-hnV][-e&l ...
- javasctipt显示几分钟前、几天前等
jsp页面: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> ...
- php join函数使用,很是方便
以前数组转换成用逗号隔开的字符串都是自己写一个数组,最后还要去除多余的一个逗号,好麻烦. 无意中发现join函数,原来一句话就可以了. $_array = array('a','b','c','d', ...
- Android开发心得(转)
前言: 很早以前,就听人说过android以后会火起来,作为一个前瞻性对它有所了解会是一个转型的好机会,javaweb太成熟饱和了,现在市面上各种android手机层出不穷,网上各种android视频 ...
- [001]const和指针
很经典的: const int* p: int* const p: 前者表示指针指向的值是const,指向的值不可变,但是指针本身是可变的:后者表示改指针是const,指针不可变,但是指向的值是可变的 ...
- 【排障】编译安装Mysql并使用自启动脚本mysqld后报错
本文用于记录在某次个人实验搭建DZ论坛,在编译安装部署mysql环节时出的错到最终排除错误的过程, 前面采用DZ官网所采用的编译安装mysql的过程就省去,主要从报错处开始讲述. (题外话,经此一役后 ...