POJ2127 Greatest Common Increasing Subsequence
给定两个 整数序列,求LCIS(最长公共上升子序列)
dp[i][j]表示A的A[1.....i]与B[1.....j]的以B[j]为结尾的LCIS。
转移方程很简单
当A[i]!=B[j] dp[i][j]=dp[i-1][j]
else dp[i][j]=max(dp[i][k]+1) k<j A[i]>B[k]
朴素实现O(n^3)
通过标记最大值的方法可以优化到O(n^2)
代码很简单
#include<stdio.h>
#include<stdlib.h>
#include<string>
#include<vector>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=500+5;
int dp[maxn][maxn];
int pre[maxn][maxn];
int A[maxn],B[maxn];
int n1,n2;
vector<int> getLcis()
{
memset(dp,0,sizeof(dp));
memset(pre,0,sizeof(pre));
vector<int>lcis;
for(int i=1;i<=n1;i++)
{
int k=0;
for(int j=1;j<=n2;j++)
{
if(A[i]!=B[j])dp[i][j]=dp[i-1][j];
if(A[i]>B[j]&&dp[i][j]>dp[i][k])k=j;
if(A[i]==B[j])
{
dp[i][j]=dp[i][k]+1;
pre[i][j]=k;
}
}
}
int ans=-1,x=n1,y=0;
for(int i=1;i<=n2;i++)
{
if(dp[n1][i]>ans)
{
ans=dp[n1][i];
y=i;
}
}
lcis.resize(ans);
int cnt=1;
while(dp[x][y])
{
if(A[x]!=B[y])x--;
else{
lcis[ans-cnt]=B[y];
cnt++;
y=pre[x][y];
}
}
return lcis;
}
int main()
{freopen("t.txt","r",stdin);
scanf("%d",&n1);
for(int i=1;i<=n1;i++)
scanf("%d",&A[i]);
scanf("%d",&n2);
for(int i=1;i<=n2;i++)
scanf("%d",&B[i]);
vector<int>L=getLcis();
printf("%d\n",L.size());
for(int ii=0;ii<(int)(L.size()-1);ii++)
if(ii<L.size())printf("%d ",L[ii]); if(L.size()>0)printf("%d\n",L[L.size()-1]);
return 0;
}
POJ2127 Greatest Common Increasing Subsequence的更多相关文章
- 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 ...
- Greatest Common Increasing Subsequence hdu1423
Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536 ...
- POJ 2127 Greatest Common Increasing Subsequence
You are given two sequences of integer numbers. Write a program to determine their common increasing ...
- HDUOJ ---1423 Greatest Common Increasing Subsequence(LCS)
Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536 ...
随机推荐
- 使用 PHP + shell 生成 一键设置权限的脚本。
linux 系统 支持PHP脚本一键设置环境.shell脚本一键设置环境.那么 我今天 使用 PHP + shell 生成 一键设置权限的脚本. 举例子:linux服务器 一键配置discuz网站环 ...
- Buffer.concat()
Buffer.concat(list[, totalLength]) Node.js FS模块方法速查 list {Array} 需要连接的 Buffer 对象数组 totalLength {Numb ...
- js 小练习
js 学习之路代码记录 js 加载时间线 1.创建Document对象,开始解析web页面.解析HTML元素和他们的文本内容后添加Element对象和Text节点到文档中.这个阶段document.r ...
- PAT 1077. 互评成绩计算
PAT 1077. 互评成绩计算 在浙大的计算机专业课中,经常有互评分组报告这个环节.一个组上台介绍自己的工作,其他组在台下为其表现评分.最后这个组的互评成绩是这样计算的:所有其他组的评分中,去掉一个 ...
- Maxscale安装-读写分离(1)
前言 关于MySQL中间件的产品也很多,之前用过了360的Atlas.玩过MyCat.这边我选择 Maxscale的原因就是功能能满足需求,也看好他的未来发展. 其实有关于如何安装 Maxscale的 ...
- ie下php session不能用(域名的合法定义)
今天遇到了一个奇怪的问题.应用程序的后台ie下居然无法登陆,老是提示验证码不正确,明明输入是正确的.于是抓包.测试.调试,最终发现罪魁祸首phpsessionid在ie下没有办法写入.研究了一下,发现 ...
- Bzoj3060 [Poi2012]Tour de Byteotia
3060: [Poi2012]Tour de Byteotia Time Limit: 30 Sec Memory Limit: 256 MBSubmit: 251 Solved: 161 Des ...
- Xterm256终端颜色的名称
hi x016_Grey0 ctermfg=16 guifg=#000000 "rgb=0,0,0 hi x017_NavyBlue ctermfg=17 guifg=#00005f &qu ...
- Swoole 入门学习(二)
Swoole 入门学习 swoole 之 定时器 循环触发:swoole_timer_tick (和js的setintval类似) 参数1:int $after_time_ms 指定时间[毫秒] ...
- Educational Codeforces Round 45 (Rated for Div. 2) C、D
C. Bracket Sequences Concatenation Problem time limit per test 2 seconds memory limit per test 256 ...