#include <iostream>
#define MAXN 501
using namespace std; int a[MAXN],b[MAXN],ans[MAXN]; int GCIS(int l1, int *a, int l2, int *b); int main()
{
//freopen("acm.acm","r",stdin);
int l_1;
int l_2;
int i;
int j;
int ans_max;
// cin>>l_1;
scanf("%d",&l_1); for(i = ; i < l_1; ++ i)
{
// cin>>a[i];
scanf("%d",&a[i]);
} // cin>>l_2;
scanf("%d",&l_2);
for(i = ; i < l_2; ++ i)
{
//cin>>b[i];
scanf("%d",&b[i]);
} cout<<(ans_max = GCIS(l_1,a,l_2,b) )<<endl; for(i = ; i < ans_max; ++ i)
{
cout<<ans[i]<<" ";
}
cout<<endl; } /////////////////////////////////////
//最长公共上升子序列~
/////////////////////////////////////
int GCIS(int l1, int *a, int l2, int *b)//ans[0...DP[max]-1]为序列,最长公共递增子序列!
{
int f[MAXN+][MAXN+];
int DP[MAXN+];
int i,j,k,max;
memset(f,,sizeof(f));
memset(DP,,sizeof(DP));
for (i=;i<=l1;i++)
{
k=;
for(int kk = ;kk <= l2;++ kk)
{
f[i][kk] = f[i-][kk];
}
for (j=;j<=l2;j++)
{
if(b[j-] < a[i-] && DP[j] > DP[k])
k=j;
if(b[j-]==a[i-]&&DP[k]+>DP[j])
{
DP[j]=DP[k]+;
f[i][j]=i*(l2+)+k;
}
}
}
max=;
for(i=;i<=l2;i++)
{
if (DP[i]>DP[max])
max=i;
}
i=l1*l2+l1+max;
for(j = DP[max];j > ;j --)
{
ans[j-] = b[i%(l2+)-];
i=f[i/(l2+)][i%(l2+)];
}
return DP[max];
}
///////////////////////////////////////////////////
//返回值是子序列的容量,容器为ans[]
///////////////////////////////////////////////////

关注我的公众号,当然,如果你对Java, Scala, Python等技术经验,以及编程日记,感兴趣的话。

技术网站地址: vmfor.com

POJ 2127的更多相关文章

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

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

  2. 【noi 2.6_2000】&【poj 2127】 最长公共子上升序列 (DP+打印路径)

    由于noi OJ上没有Special Judge,所以我是没有在这上面AC的.但是在POJ上A了. 题意如标题. 解法:f[i][j]表示a串前i个和b串前j个且包含b[j]的最长公共上升子序列长度 ...

  3. poj 2127 LCIS 带路径输出

    这个题   用一维 为什么错了: 因为 用一维 dp 方程肯定也是一维:但是有没有想,第 i 个字符更新了 j 位置的最优结果,然后 k 字符又一次更新了  j 位置的最优值,然后  我的结果是  i ...

  4. POJ 2127 最长公共上升子序列

    动态规划法: #include <iostream> #include <cstdio> #include <fstream> #include <algor ...

  5. POJ 2127 Greatest Common Increasing Subsequence

    You are given two sequences of integer numbers. Write a program to determine their common increasing ...

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

  7. 最长公共上升子序列 (poj 2127) (Greatest Common Increasing Subsequence)

    \(Greatest Common Increasing Subsequence\) 大致题意:给出两个长度不一定相等的数列,求其中最长的公共的且单调递增的子序列(需要具体方案) \(solution ...

  8. ACM - 动态规划专题 题目整理

    CodeForces 429B  Working out 预处理出从四个顶点到某个位置的最大权值,再枚举相遇点,相遇的时候只有两种情况,取最优解即可. #include<iostream> ...

  9. LICS O(n*m)+前驱路径

    LICS:最长公共上升子序列: 一般令f[i][j]表示a串前i位,b串以j结尾的LICS长度.于是,答案为:max(1~m)(f[n][i]); 朴素做法:O(n^3) 相等时,从1~j-1枚举最大 ...

随机推荐

  1. Linux: uid/euid/suid的关系

    三种进程用户的简单解释:三种用户/组ID:uid/gid: 实际用户/组IDeuid/egid: 有效用户/组ID, 进程执行某个应用的用户/组ID.suid/sgid: 设置用户/组ID, 应用所属 ...

  2. UITableView表视图以及重建机制

    表视图UITableView   表视图UITableView,是IOS中最重要的视图,随处可见 表视图通常用来管理一组具有相同数据结构的数据 UITableView继承自UIScrollView,所 ...

  3. MySQL命令输入错误 取消命令

    mysql命令行输入错误字符需要取消执行时,可在分号“:”出现前输入“\c”来取消该条命令,注意c前边的“\”.   1 2 3 4 mysql> select -> user() -&g ...

  4. UIImageView swift

    // // ViewController.swift // UILabelTest // // Created by mac on 15/6/23. // Copyright (c) 2015年 fa ...

  5. 推荐一款系统软件:Unity tweak tool

    功能很多慢慢体会 在软件中心搜索unity tweak tool安装

  6. 0x0A和0x0D

    这里主要是在windows下面做的小实验,linux没有试 先贴源码 #include <iostream> #include <string> #include <st ...

  7. PHP错误The server encountered an internal error or misconfiguration and was unable to complete your re

    我的笔记本电脑上的环境安装了很多次,但是运行项目时总是会报The server encountered an internal error or misconfiguration and was un ...

  8. Installing Ruby 1.9.3 on Ubuntu 12.04 Precise Pengolin (without RVM)

    02 MAY, 2012 The new Ubuntu release has just rolled around and with it a slew of new packages. Perso ...

  9. bzoj 3171 费用流

    每个格拆成两个点,出点连能到的点的入点,如果是箭头指向 方向费用就是0,要不就是1,源点连所有出点,所有入点连 汇点,然后费用流 /********************************** ...

  10. C++ Tempatet之模板模型

    模板一共有三种类型: 1.第一种包含模型:包含模型是讲模板的定义和声明都放在头文件里(注:一般我们写的代码是将声明放在头文件里,实现放在cpp里,防止产生两份实现代码) 缺点:包含模型会增加代码的量. ...