动态规划法:

#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 最长公共上升子序列的更多相关文章

  1. 【简单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 题目:给你两个数字序列,求出这两个序列的最长公共上升子序列.输出最长的长度,并打表 ...

  2. POJ 1458 最长公共子序列(dp)

    POJ 1458 最长公共子序列 题目大意:给出两个字符串,求出这样的一 个最长的公共子序列的长度:子序列 中的每个字符都能在两个原串中找到, 而且每个字符的先后顺序和原串中的 先后顺序一致. Sam ...

  3. 最长公共上升子序列(codevs 2185)

    题目描述 Description 熊大妈的奶牛在小沐沐的熏陶下开始研究信息题目.小沐沐先让奶牛研究了最长上升子序列,再让他们研究了最长公共子序列,现在又让他们要研究最长公共上升子序列了. 小沐沐说,对 ...

  4. 最长公共上升子序列(LCIS)

    最长公共上升子序列慕名而知是两个字符串a,b的最长公共递增序列,不一定非得是连续的.刚开始看到的时候想的是先用求最长公共子序列,然后再从其中找到最长递增子序列,可是仔细想一想觉得这样有点不妥,然后从网 ...

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

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

  6. [CodeForces10D]LCIS(最长公共上升子序列) - DP

    Description 给定两个数列,求最长公共上升子序列,并输出其中一种方案. Input&Output Input 第一行一个整数n(0<n<=500),数列a的长度. 第二行 ...

  7. poj 2774 最长公共子串 后缀数组

    Long Long Message Time Limit: 4000MS   Memory Limit: 131072K Total Submissions: 25752   Accepted: 10 ...

  8. 最长递增子序列(lis)最长公共子序列(lcs) 最长公共上升子序列(lics)

    lis: 复杂度nlgn #include<iostream> #include<cstdio> using namespace std; ],lis[],res=; int ...

  9. codevs 2185 最长公共上升子序列

    题目链接: codevs 2185 最长公共上升子序列codevs 1408 最长公共子序列 题目描述 Description熊大妈的奶牛在小沐沐的熏陶下开始研究信息题目.小沐沐先让奶牛研究了最长上升 ...

随机推荐

  1. mac链接linux终端,shell脚本发布代码

    项目的业务需求:从mac端直接连上linux服务终端,并发布相关的代码 一.使用ssh链接上linux服务端 1.cd ~/.ssh 2.vi config,按照下面的内容配置config文件,然后: ...

  2. .NEL IL实现对象深拷贝

    对于深拷贝,通常的方法是将对象进行序列化,然后再反序化成为另一个对象.例如在stackoverflow上有这样的解决办法:https://stackoverflow.com/questions/785 ...

  3. 最近跟mysql的自动shutdown干上了。。。

    数据库变大之后,又开始不定时shutdown了. 搜了半天,也没啥用, 根据谋篇写的内容,改了下一个参数   vi /etc/my.cnf 修改innodb_buffer_pool_size = 16 ...

  4. ASP.NET Core 与 Vue.js 服务端渲染

    http://mgyongyosi.com/2016/Vuejs-server-side-rendering-with-aspnet-core/ 原作者:Mihály Gyöngyösi 译者:oop ...

  5. Java _分页Jdbc 版

    人生得意须尽欢,莫使金樽空对月. 先天下之忧而忧,后天下之乐而乐. 大东北的天气已经渐入佳境了,在夜深人静的时候,随着鼠标的移动,键盘清脆的声音,开启了今天的睡前代码工程!今天聊聊JDBC版本的分页, ...

  6. hibernate flushMode 错误

    1 十一月 15, 2017 10:13:36 上午 org.apache.struts2.dispatcher.Dispatcher error 2 严重: Exception occurred d ...

  7. Spark之MapReduce原理

    参考http://www.cnblogs.com/wuyudong/p/mapreduce-principle.html MapReduce   我们来拆开看: Mapping(映射)对集合里的每个目 ...

  8. 表字符集latin1,client如何查看中文字符

    表结构如下: . row *************************** Table: my_data_7e Create Table: CREATE TABLE `my_data_7e` ( ...

  9. .net core2.0下使用Identity改用dapper存储数据

    前言. 已经好多天没写博客了,鉴于空闲无聊之时又兴起想写写博客,也当是给自己做个笔记.过了这么些天,我的文笔还是依然那么烂就请多多谅解了.今天主要是分享一下在使用.net core2.0下的实际遇到的 ...

  10. Python StringIO与BytesIO、类文件对象

    StringIO与BytesIO StringIO与BytesIO.类文件对象的用途,应用场景,优.缺点. StringIO StringIO 是io 模块中的类,在内存中开辟的一个文本模式的buff ...