【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 ...
随机推荐
- OTRS
更新OTRS root@localhost密码: sudo -u otrs /opt/otrs/bin/otrs.Console.pl Admin::User::SetPassword root@lo ...
- git查看添加删除远程仓库
查看远程仓库 git remote -v 删除远程仓库 git remote remove origin 添加远程仓库 git remote add origin 仓库地址 关联远程分支 重新关联远程 ...
- Java调用XML的方法:DocumentBuilderFactory
(1)首先得到:得到 DOM 解析器的工厂实例 DocumentBuilderFactory domfac=DocumentBuilderFactory.newInstance(); (2)然后从 D ...
- Spring Bean注册解析(二)
在上文Spring Bean注册解析(一)中,我们讲解了Spring在注册Bean之前进行了哪些前期工作,以及Spring是如何存储注册的Bean的,并且详细介绍了Spring是如何解析 ...
- TeamWork#1,Week 5,Suggestions for Team Project
我们团队联系到了我们六班的直系学长,并向他咨询了软件工程基础这门课的团队项目相关的问题.他们团队的名字命名为Z-XML,团队中的几个学长也都是我平时所熟识的.虽然学长已经大四,忙着考研工作等各种事务, ...
- iOS自学-混合编程
OC调用swift,引入头文件 #improt "工程名字-swift.h" swift调用OC,在桥梁文件里面引入OC文件 的头文件 尽情混合编程吧...
- 《校友聊—方便使用之NABCD》
<校友聊—方便使用之NABCD> 方便使用特点的分析与总结: N:由于一些软件的人机交互性很差,人性化性能低,使用不方便,故,鉴于此需求:A:用户可根据系统界面的友好提示一步步进行:B:提 ...
- ns3 回调机制
(1)目的:为了实现两个模块之间的通信(这两个模块没有任何依赖关系) (2) C语言中的函数指针 int (*a)(int q) = 0; //声明一个函数指针a,初始值设为0 //. //. //. ...
- Head First Java & 异常
- jquery 取消全选和全选功能 不全选
代码如下 function ckSelectAll() { if ($('#ckSelectAll').is(':checked') == true) { $("INPUT[name='ch ...