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的更多相关文章

  1. 【题解】Greatest Common Increasing Subsequence

    [题解]Greatest Common Increasing Subsequence vj 唉,把自己当做DP入门选手来总结这道题吧,我DP实在太差了 首先是设置状态的技巧,设置状态主要就是要补充不漏 ...

  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 ...

  3. HDOJ 1423 Greatest Common Increasing Subsequence 【DP】【最长公共上升子序列】

    HDOJ 1423 Greatest Common Increasing Subsequence [DP][最长公共上升子序列] Time Limit: 2000/1000 MS (Java/Othe ...

  4. HDU 1423 Greatest Common Increasing Subsequence LCIS

    题目链接: 题目 Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...

  5. POJ 2127 Greatest Common Increasing Subsequence -- 动态规划

    题目地址:http://poj.org/problem?id=2127 Description You are given two sequences of integer numbers. Writ ...

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

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

  7. ZOJ 2432 Greatest Common Increasing Subsequence(最长公共上升子序列+路径打印)

    Greatest Common Increasing Subsequence 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problem ...

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

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

  9. 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 ...

随机推荐

  1. Haproxy介绍、安装与配置

    Haproxy技术详解一. 介绍HAProxy是一个使用C语言编写的自由及开放源代码软件,其提供高可用性.负载均衡,以及基于TCP和HTTP的应用程序代理.HAProxy特别适用于那些负载特大的web ...

  2. Vue Cli 中使用 Karma / Chrome 执行样式相关单元测试

    在 GearCase 开源项目 中,我使用了 Vue Cli 的默认测试框架.因此和样式相关的东西,都无法进行测试.因为它并不类似于无头浏览器,而是存在于虚拟内存之中. 现状 在如下 button.s ...

  3. Netty源码分析第5章(ByteBuf)---->第1节: AbstractByteBuf

    Netty源码分析第五章: ByteBuf 概述: 熟悉Nio的小伙伴应该对jdk底层byteBuffer不会陌生, 也就是字节缓冲区, 主要用于对网络底层io进行读写, 当channel中有数据时, ...

  4. To Do List | 事实上是咕咕咕计划

    1.写一两篇关于数学的博文 类似于这种反演啥的或者说是FFT一些更本质的东西趴...反正是我根本不会的东西 再写一点自己会的东西趴...(好像也只有什么课本上的东西讲讲了,不过应该会写一些自己曾经发现 ...

  5. Java调用XML的方法:DocumentBuilderFactory

    (1)首先得到:得到 DOM 解析器的工厂实例 DocumentBuilderFactory domfac=DocumentBuilderFactory.newInstance(); (2)然后从 D ...

  6. 基于LiFi可见光通信技术的研究及应用转化调查

    这个仅是本人的部分调研结果,有同行做可见光研究的可以联系交流,QQ:391349683 

  7. 为什么每次进入命令都要重新source /etc/profile 才能生效?

    https://segmentfault.com/q/1010000005981201

  8. 《Spring1之第四次站立会议》

    <第四次站立会议> 昨天:我把小组成员找到的写关于登录界面的代码加到了我的项目工程里,并对它有了一定的了解,已经能够编译运行了,得到了登陆的界面: 今天:试着做了一下主框架里的在线人数的显 ...

  9. 新手学ajax2

    今天主要解决了一个困扰两天的ajax问题,就是关于从服务器获取数据时的同步和异步问题 , xhr.open("GET", url,false): 这里有三个参数“GET”表示获取的 ...

  10. My Sql数据库设置环境变量和字符集

    一.踩坑背景 之前开发中一直用的是sql  server 数据库,最近接到公司一个老的项目,用的my sql数据库做的,功能做了一大部分,现在客户要求对原程序和数据库进行服务器的迁移工作.产品经理给出 ...