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 ...
随机推荐
- centos6 用户登陆管理
查看当前登陆有哪些用户,在做什么 [root@web01 ~]# w :: up :, users, load average: 0.00, 0.00, 0.00 USER TTY FROM LOGI ...
- [安装] mac安装PHP7经历
背景 前几天在mac上跑workrman,由于workerman需要开启多个进程,多进程需要pcntl扩展的支持,我之前那个brew安装的php71没有这个扩展,就直接卸载了php71,然后想下载源码 ...
- php7 安装swoole扩展
昨天无意中看到一篇关于直播的视频教程 里面讲到了swoole,对于这个东西我相信大家(接近1年phper)都是听过它,但没有真正去用它,当然也是不知道如何使用(me too). 此处总结一下(借鉴了几 ...
- c:foreach 标签 varStatus的使用
<c:forEach items="${MedicalDoctoList }" var="medicalDoctor" varStatus="s ...
- table中JS选取行列
<table border="1" align="center" style="width: 500px" id="doct ...
- Codeforce 741B Arpa's weak amphitheater and Mehrdad's valuable Hoses(并查集&分组背包)
题意: 给定n个价值为b 花费为w的物品, 然后某些物品是属于同一个组的, 给定一个花费限制V, 求在小于等于V的情况下取得到的价值最大为多少,能对于同一个组的物品,要么全取,要么只取一个. 分析: ...
- Python字典(Dictionary)
Python字典 字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包括在花括号({})中 ,格式如下所示: d = {key1 : value1, ...
- 慕课笔记利用css进行布局【三列布局】
三个div中间自适应,两侧固定大小 <html> <head> <title>三列布局</title> <style type="tex ...
- BNUOJ 1585 Girls and Boys
Girls and Boys Time Limit: 5000ms Memory Limit: 10000KB This problem will be judged on PKU. Original ...
- noip模拟赛 Nephren Ruq Insania
题目背景 大样例下发链接: https://pan.baidu.com/s/1nuVpRS1 密码: sfxg 注意:本题大样例4的输出文件修改为 https://pan.baidu.com/s/1b ...