Sample Input

5
1 4 2 5 -12
4
-12 1 2 4

Sample Output

2
1 4 题目:给你两个数字序列,求出这两个序列的最长公共上升子序列。
输出最长的长度,并打表输出。可能存在多种正确答案,故此题是special judge! 分析:dp[i][j] : A[1...i]和B[1...j]的公共上升子序列中以B[j]为结尾的最长的长度。
如果A[i] != B[j], 则dp[i][j]=d[i-1][j]; 也就是说当前这个A[i]是没效用的。
如果A[i] = B[j], 则dp[i][j]=max( dp[i][k]+1 ),其中 k<j 且 A[i]>B[k]
时间复杂度O(n^2) 注意:
n1 n2位两个序列的长度,
A[] B[]为两个序列数组,
ans 全局变量 最长公共子序列的长度值
lcis 最长公共上升子序列 打表存储 代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <iostream>
#include <string>
#include <algorithm> using namespace std; int n1, n2;
int A[510], B[510];
int dp[510][510];
int pre[510][510];
int lcis[510];
int ans; void get_LCIS()
{
memset(dp, 0, sizeof(dp));
memset(pre, 0, sizeof(pre));
int i, j;
for(i=1; i<=n1; i++){
int 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;
}
}
}
ans=-1;
int x=n1, y=0;
for(i=1; i<=n2; i++){
if(dp[n1][i]>ans){
ans=dp[n1][i]; y=i;
}
}
int cnt=1;
while(dp[x][y]){
if(A[x] != B[y]) x--;
else{
lcis[ans-cnt]=B[y];
cnt++;
y=pre[x][y];
}
}
} int main()
{
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]); get_LCIS();
printf("%d\n", ans );
for(int i=0; i<ans; i++)
printf("%d%c", lcis[i], i==ans-1?'\n':' '); return 0;
}
												

【简单dp】poj 2127 Greatest Common Increasing Subsequence【最长公共上升子序列】【模板】的更多相关文章

  1. HDU 1423 Greatest Common Increasing Subsequence(最长公共上升LCIS)

    HDU 1423 Greatest Common Increasing Subsequence(最长公共上升LCIS) http://acm.hdu.edu.cn/showproblem.php?pi ...

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

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

  3. POJ 2127 Greatest Common Increasing Subsequence

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

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

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

  5. LCIS POJ 2172 Greatest Common Increasing Subsequence

    题目传送门 题意:LCIS(Longest Common Increasing Subsequence) 最长公共上升子序列 分析:a[i] != b[j]: dp[i][j] = dp[i-1][j ...

  6. POJ 1423 Greatest Common Increasing Subsequence【裸LCIS】

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1423 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  7. HDOJ 1423 Greatest Common Increasing Subsequence 【DP】【最长公共上升子序列】

    HDOJ 1423 Greatest Common Increasing Subsequence [DP][最长公共上升子序列] Time Limit: 2000/1000 MS (Java/Othe ...

  8. 【题解】Greatest Common Increasing Subsequence

    [题解]Greatest Common Increasing Subsequence vj 唉,把自己当做DP入门选手来总结这道题吧,我DP实在太差了 首先是设置状态的技巧,设置状态主要就是要补充不漏 ...

  9. HDU 1423 Greatest Common Increasing Subsequence LCIS

    题目链接: 题目 Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...

随机推荐

  1. C# winform中 选择文件和保存文件

    转载自https://blog.csdn.net/qq_31788297/article/details/62047952 我们在使用桌面软件的时候经常会使用到选择文件并打开和另存为等的窗口,这样方便 ...

  2. iframe详解

    如何查看是否为iframe *使用FireFox组件firebug->firepath 1.Top Window:可直接定位 2.iframe#i:根据id定位 定位方法: switch_to. ...

  3. java用iText导出word文档

    1.需要导入的jar包 2.导出word并下载其实是分两步的. 第一步是将需要导出的数据导出(上传)到服务器上 第二步是将服务器上的文档下载到本地 3. 第一步.上传文档 (1)设置响应信息以及构造上 ...

  4. hdu1174(3维射线与圆是否相交)

    简单的题意,要注意z2 = h2*0.9-r2 #include <iostream> #include <cmath> #include <vector> #in ...

  5. K - Children of the Candy Corn(待续)

    K - Children of the Candy Corn Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d ...

  6. mock数据(模拟后台数据)

    mock数据(模拟后台数据) - Emily恩 - 博客园 https://www.cnblogs.com/enboke/p/vue.html Mock.js http://mockjs.com/ 前 ...

  7. TypeError: save() missing 1 required positional argument: 'self'

    RT,在创建模型对象的时候,提示TypeError: save() missing 1 required positional argument: 'self' 解决办法:在创建模型对象的时候需要加上 ...

  8. Django的admin定制

    1,models编写 #encoding=utf-8 from django.db import models # Create your models here. class BookInfo(mo ...

  9. ZRGGBS00 GGB1替代问题

    ZRGGBS00ZRGGBS00ZRGGBS00 和Validation不同的是,Validation只做检测,一般不做相应数据的修改,Substitution弥补了这反面的缺陷,它和user exi ...

  10. javascript高级语法三

    一.js的正则表达式 1.什么是正则表达式 正则表达式(regular expression)是一个描述字符模式的对象,ECMAScript的RegExp类表示正则表达式,而String和RegExp ...