#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. 浅议iOS网络数据解析

    /*------------------------------------ 数据解析: 1.JSON数据 --------------------------------*/ 重点:1.什么是JSO ...

  2. Kettle行列转换

    Kettle在控件中拥有行列转换功能,但是行列转换貌似是弄反了. 一.行转列 1.数据库脚本 create TABLE StudentInfo ( studentno int, subject ), ...

  3. c语言的结构体字节数统计

    struct结构体的字节数 等于 结构体中最大类型的字节数的倍数. 如: typedef struct Student{    short id; //2个字节    char name[15]; / ...

  4. 慎把“DataContext”静态化 或则单例

    之前在项目里由于把DataContext静态化,最后在测试阶段发现了很多奇怪的问题,后来经过同事的指点 然后上网搜了一翻终于发现 MSDN上说:   "请不要试图重用 DataContext ...

  5. VC++编程中获取系统时间

    <span style="white-space:pre"> </span>总结了在程序中如何获得系统时间的方法 void CGetSystenTimeDl ...

  6. [无人值守安装操作系统]__RHEL6__FTP+TFTP+DHCP+Kickstart+PXE

    实验环境 1.实验平台:VMware Workstation 10 2.实验OS:RHEL6 3.服务器A: (1) 10.0.10.158 (2) DHCP/FTP/TFTP (3) 有可使用的yu ...

  7. 思维认知-读mindhacks杂记

    1. 导语 无意中浏览知乎,搜索到了mindhacks.cn这个个人geek的网址.mindhacks博主本人是牛人程序员一枚,但他的博客主题涵盖的主要内容确是思维改变生活. 博客链接地址:http: ...

  8. STL之map

    参见http://www.cplusplus.com/reference/map/map/ template < class Key,                               ...

  9. Understanding Convolutions【转】

    Understanding Convolutions In a previous post, we built up an understanding of convolutional neural ...

  10. HTML统一资源定位器

    w3c 更好的解释 在OSGi in action中安装bundle时要加文本传输协议,要不然shell判断不出来