POJ 2127 最长公共上升子序列
动态规划法:
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#define PI acos(-1.0)
#define mem(a,b) memset(a,b,sizeof(a))
#define sca(a) scanf("%d",&a)
#define sc(a,b) scanf("%d%d",&a,&b)
#define pri(a) printf("%d\n",a)
#define lson i<<1,l,mid
#define rson i<<1|1,mid+1,r
#define MM 1004
#define MN 1008
#define INF 100000007
#define eps 1e-7
using namespace std;
typedef long long ll;
typedef unsigned long long ULL;
int n1,n2,a[MM],b[MM];//a,b序列及其长度
int dp[MM][MM],pre[MM][MM],lcis[MM];//lcis为最长公共上升子序列
int ans=-1;//ans为最长公共上升子序列长度
void getLcis()
{
int x=n1,y=0,cnt=1,i,j,k;
mem(dp,0),mem(pre,0);
for(i=1;i<=n1;i++)
{
k=0;
for(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;
}
}
for(i=1;i<=n2;i++)
if(dp[n1][i]>ans) ans=dp[n1][i],y=i;
while(dp[x][y])
{
if(a[x]!=b[y]) x--;
else lcis[ans-cnt]=b[y],cnt++,y=pre[x][y];
}
}
int main()
{
sca(n1);
for(int i=1;i<=n1;i++)
sca(a[i]);
sca(n2);
for(int i=1;i<=n2;i++)
sca(b[i]);
getLcis();
pri(ans);
for(int i=0;i<ans;i++)
printf("%d ",lcis[i]);
puts("");
return 0;
}
POJ 2127 最长公共上升子序列的更多相关文章
- 【简单dp】poj 2127 Greatest Common Increasing Subsequence【最长公共上升子序列】【模板】
Sample Input 5 1 4 2 5 -12 4 -12 1 2 4 Sample Output 2 1 4 题目:给你两个数字序列,求出这两个序列的最长公共上升子序列.输出最长的长度,并打表 ...
- POJ 1458 最长公共子序列(dp)
POJ 1458 最长公共子序列 题目大意:给出两个字符串,求出这样的一 个最长的公共子序列的长度:子序列 中的每个字符都能在两个原串中找到, 而且每个字符的先后顺序和原串中的 先后顺序一致. Sam ...
- 最长公共上升子序列(codevs 2185)
题目描述 Description 熊大妈的奶牛在小沐沐的熏陶下开始研究信息题目.小沐沐先让奶牛研究了最长上升子序列,再让他们研究了最长公共子序列,现在又让他们要研究最长公共上升子序列了. 小沐沐说,对 ...
- 最长公共上升子序列(LCIS)
最长公共上升子序列慕名而知是两个字符串a,b的最长公共递增序列,不一定非得是连续的.刚开始看到的时候想的是先用求最长公共子序列,然后再从其中找到最长递增子序列,可是仔细想一想觉得这样有点不妥,然后从网 ...
- ZOJ 2432 Greatest Common Increasing Subsequence(最长公共上升子序列+路径打印)
Greatest Common Increasing Subsequence 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problem ...
- [CodeForces10D]LCIS(最长公共上升子序列) - DP
Description 给定两个数列,求最长公共上升子序列,并输出其中一种方案. Input&Output Input 第一行一个整数n(0<n<=500),数列a的长度. 第二行 ...
- poj 2774 最长公共子串 后缀数组
Long Long Message Time Limit: 4000MS Memory Limit: 131072K Total Submissions: 25752 Accepted: 10 ...
- 最长递增子序列(lis)最长公共子序列(lcs) 最长公共上升子序列(lics)
lis: 复杂度nlgn #include<iostream> #include<cstdio> using namespace std; ],lis[],res=; int ...
- codevs 2185 最长公共上升子序列
题目链接: codevs 2185 最长公共上升子序列codevs 1408 最长公共子序列 题目描述 Description熊大妈的奶牛在小沐沐的熏陶下开始研究信息题目.小沐沐先让奶牛研究了最长上升 ...
随机推荐
- python实现查有道词典
因为要考英语四级,所以我今天一大早就起来被英语单词,但是作为英语渣渣的我,只能是在网页上挨个查单词的意思.查的多了,心生厌倦,便想着如何才能在终端下查单词,那样速度不就很快了? NOW,我仔细观察每次 ...
- java与OC比较
转自:http://blog.sina.com.cn/s/blog_93742d0d010165qi.html 1.Cocoa是什么?Cocoa是使用OC语言编写的工具包,里面有大量的类库.结构体,说 ...
- Power BI本地部署(10月正式版)
Power BI安装环境要求 Windows 7/Windows Server 2008 R2 或更高版本 .NET 4.5 或更高版本 Internet Explorer 9 或更高版本 内存 (R ...
- c#中获取路径方法
要在c#中获取路径有好多方法,一般常用的有以下五种: //获取应用程序的当前工作目录. String path1 = System.IO.Directory.GetCurrentDirectory() ...
- 为什么选择使用Sass而不是Less?
这篇文章主要解答以下几个问题,供前端开发者的新手参考. 1.什么是Sass和Less? 2.为什么要使用CSS预处理器? 3.Sass和Less的比较 4.为什么选择使用Sass而不是Less? 什么 ...
- [转载] linux中文件描述符fd和文件指针flip的理解
转载自http://www.cnblogs.com/Jezze/archive/2011/12/23/2299861.html 简单归纳:fd只是一个整数,在open时产生.起到一个索引的作用,进程通 ...
- vue2.0 带头冲锋(先穿鞋)
事先说明:这次截图纯手工敲打,可不容易了.刚学会站直,不穿鞋,不得直接摔个狗食屎.(皮糙肉厚也顶不住啊). 废话不多了 !开始学基础.学过anguler1.0 ,会比较容易学vue. 溶解使用的是 v ...
- iOS设置圆角的方法及指定圆角的位置
在iOS开发中,我们经常会遇到设置圆角的问题, 以下是几种设置圆角的方法: 第一种方法: 通过设置layer的属性 代码: UIImageView *imageView = [[UIImageView ...
- python中print()函数的“,”与java中System.out.print()函数中的“+”
python中的print()函数和java中的System.out.print()函数都有着打印字符串的功能. python中: print("hello,world!") 输出 ...
- Bitmap.Config 说明 ALPHA_8 ARGB_4444 ARGB_8888 RGB_565
这篇文章的目的是了解Bitmap.Config 你可以在使用这个方法的时候会遇到 Bitmap android.graphics.Bitmap.createBitmap(int width, int ...