【poj2127】 Greatest Common Increasing Subsequence
http://poj.org/problem?id=2127 (题目链接)
题意
计算两个序列$a$和&b$的最长公共上升子序列。
Solution
爸爸的$n^3$算法莫名其妙RE了,不爽之下学习了一发$n^2$的。
http://www.cnblogs.com/dream-wind/archive/2012/08/25/2655641.html
$f[i][j]$表示序列$a$的前$i$位,序列$b$的前$j$位,以$b_j$结尾的最长公共上升子序列长度。
转移:$$f[i][j]=f[i-1][j](a_i!=b_j)$$
$$f[i][j]=max_{1<=k<j}f[i-1][k]+1(a_i=b_j)$$
这样直接做的话是$n^3$的,我们用前缀最大值优化一下,然后记录方案即可。
代码
// poj2127
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cmath>
#define LL long long
#define inf (1ll<<60)
#define Pi acos(-1.0)
#define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
using namespace std; const int maxn=1000;
int a[maxn],b[maxn],f[maxn][maxn],p[maxn][maxn],ans[maxn];
int n,m; int main() {
while (scanf("%d",&n)!=EOF) {
for (int i=1;i<=n;i++) scanf("%d",&a[i]);
scanf("%d",&m);
for (int i=1;i<=m;i++) scanf("%d",&b[i]);
int sum=0,ai,aj;
for (int i=1;i<=n;i++) {
int mx=0,mj=0;
for (int j=1;j<=m;j++) {
f[i][j]=f[i-1][j];
p[i][j]=-1;
if (b[j]<a[i] && f[i-1][j]>mx) mx=f[i-1][j],mj=j;
else if (a[i]==b[j]) f[i][j]=mx+1,p[i][j]=mj;
if (sum<f[i][j]) sum=f[i][j],ai=i,aj=j;
}
}
printf("%d\n",sum);
int tmp=sum;
for (;ai;ai--) if (p[ai][aj]>-1) ans[tmp--]=b[aj],aj=p[ai][aj];
for (int i=1;i<=sum;i++) printf("%d ",ans[i]);
puts("");
}
return 0;
}
【poj2127】 Greatest Common Increasing Subsequence的更多相关文章
- 【题解】Greatest Common Increasing Subsequence
[题解]Greatest Common Increasing Subsequence vj 唉,把自己当做DP入门选手来总结这道题吧,我DP实在太差了 首先是设置状态的技巧,设置状态主要就是要补充不漏 ...
- 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 ...
- HDOJ 1423 Greatest Common Increasing Subsequence 【DP】【最长公共上升子序列】
HDOJ 1423 Greatest Common Increasing Subsequence [DP][最长公共上升子序列] Time Limit: 2000/1000 MS (Java/Othe ...
- HDU 1423 Greatest Common Increasing Subsequence LCIS
题目链接: 题目 Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...
- POJ 2127 Greatest Common Increasing Subsequence -- 动态规划
题目地址:http://poj.org/problem?id=2127 Description You are given two sequences of integer numbers. Writ ...
- HDOJ 1423 Greatest Common Increasing Subsequence -- 动态规划
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1423 Problem Description This is a problem from ZOJ 2 ...
- ZOJ 2432 Greatest Common Increasing Subsequence(最长公共上升子序列+路径打印)
Greatest Common Increasing Subsequence 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problem ...
- HDU 1423 Greatest Common Increasing Subsequence(最长公共上升LCIS)
HDU 1423 Greatest Common Increasing Subsequence(最长公共上升LCIS) http://acm.hdu.edu.cn/showproblem.php?pi ...
- HDU1423:Greatest Common Increasing Subsequence(LICS)
Problem Description This is a problem from ZOJ 2432.To make it easyer,you just need output the lengt ...
随机推荐
- beego跨域请求配置
不说废话 在main函数前加入如下代码 func init() { //跨域设置 var FilterGateWay = func(ctx *context.Context) {ctx.Respons ...
- 【Ansible】ansible循环
Ansible 循环 一.简单介绍 在ansible2.5之前,大多数人使”with_XXX”类型的关键字来操作循环,但是从2.6版本开始,官方推荐是”loop”关键字代替” with_XXX”. 1 ...
- Hyperledger Fabric 中channel配置相关数据结构
channel Configuration Transaction Hyperledger Fabric区块链网络中的配置存储在一个configuration-transaction的集合中,每个ch ...
- python其他知识目录
博客目录总纲首页 基础的重要性(程序员之路) 做一个“合格”的程序员(一)——基础能力 作为一个程序员,数学对你到底有多重要 同样是程序员,为什么别人比你更优秀? ------------------ ...
- Mysql DataPacketTooBigException异常处理
在本地上运行好好,然后发布到服务器上去, 总是报错,后来查了一下日志,得到了如下的错误日志: [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] Pa ...
- 多种方法实现左右固定,中间自适应的CSS布局
布局是面试中常问的问题,尤其是这类的题目,怎么答才好呢? 大多数人的第一个方法是浮动,没错,浮动.第二个方法呢?你回答定位,没错.第三个方法呢?.... 第四个方法呢?第五个方法呢?.... 其实能想 ...
- 王者荣耀交流协会互评Beta版本及答复功能改进建议、Bug修正
互评Beta版本 欢迎来怼团队博客园安卓APP Thunder团队爱阅app 探路者团队贪吃蛇 Hello World!团队项目空天猎 答复功能改进建议 答复其他各组给出的“就现有技术和工作量,不改变 ...
- a标签的href为空的问题
在表格里写一个a标签链接刷新表格的时候,没注意,把a标签的href设置为""空字符串,导致每次刷新表格之后会再刷新一次整体页面,找了很久都没发现问题出在哪里,最后无意之间,鼠标在一 ...
- 20172319 《Java程序设计教程》 第10周学习总结
20172319 2018.05.09-05.21 <Java程序设计教程>第10周学习总结 目录 教材学习内容总结 教材学习中的问题和解决过程 代码调试中的问题和解决过程 代码托管 上周 ...
- mianshi
https://blog.csdn.net/u012557610/article/details/80350099 https://blog.csdn.net/liuqiyao_01/article/ ...