【简单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 题目:给你两个数字序列,求出这两个序列的最长公共上升子序列。
输出最长的长度,并打表输出。可能存在多种正确答案,故此题是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【最长公共上升子序列】【模板】的更多相关文章
- HDU 1423 Greatest Common Increasing Subsequence(最长公共上升LCIS)
HDU 1423 Greatest Common Increasing Subsequence(最长公共上升LCIS) http://acm.hdu.edu.cn/showproblem.php?pi ...
- POJ 2127 Greatest Common Increasing Subsequence -- 动态规划
题目地址:http://poj.org/problem?id=2127 Description You are given two sequences of integer numbers. Writ ...
- POJ 2127 Greatest Common Increasing Subsequence
You are given two sequences of integer numbers. Write a program to determine their common increasing ...
- 最长公共上升子序列 (poj 2127) (Greatest Common Increasing Subsequence)
\(Greatest Common Increasing Subsequence\) 大致题意:给出两个长度不一定相等的数列,求其中最长的公共的且单调递增的子序列(需要具体方案) \(solution ...
- LCIS POJ 2172 Greatest Common Increasing Subsequence
题目传送门 题意:LCIS(Longest Common Increasing Subsequence) 最长公共上升子序列 分析:a[i] != b[j]: dp[i][j] = dp[i-1][j ...
- 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 ...
- HDOJ 1423 Greatest Common Increasing Subsequence 【DP】【最长公共上升子序列】
HDOJ 1423 Greatest Common Increasing Subsequence [DP][最长公共上升子序列] Time Limit: 2000/1000 MS (Java/Othe ...
- 【题解】Greatest Common Increasing Subsequence
[题解]Greatest Common Increasing Subsequence vj 唉,把自己当做DP入门选手来总结这道题吧,我DP实在太差了 首先是设置状态的技巧,设置状态主要就是要补充不漏 ...
- HDU 1423 Greatest Common Increasing Subsequence LCIS
题目链接: 题目 Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...
随机推荐
- poj 2513(欧拉路径+字典树映射)
题目链接:http://poj.org/problem?id=2513 思路:题目还是很简单的,就是判断是否存在欧拉路径,我们给每个单词的头和尾映射序号,统计度数.对于给定的无向图,当且仅当图连通并且 ...
- 如何顺利解决mac下命令不管用的情况
背景: 昨晚通过brew安装了node,结果导致我的终端除了cd和ls管用外,其他的命令都不管用了,网上搜索了一大堆,结果没有一个能正确解决我的问题的,记录一下吧. 打开终端就显示: -bash: t ...
- Gradle5.x打jar包上传maven仓库
1.上传本地仓库 1.1 build.gradle 项目设置 plugins { id 'java' id 'maven' //引入maven插件 } group 'com.inkyi' //包名 v ...
- 2318: Spoj4060 game with probability Problem
2318: Spoj4060 game with probability Problem Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 356 Sol ...
- HBase架构原理详情
本文从网上看到的,自己看过了,同时收藏下!感谢分享! HBase定义 HBase 是一个高可靠.高性能.面向列.可伸缩的分布式存储系统,利用Hbase技术可在廉价PC Server上搭建 大规模结构化 ...
- 2048 worker_connections are not enough while connecting to upstream
2048 worker_connections are not enough while connecting to upstream http://mailman.nginx.org/piperma ...
- redo binlog
w https://dev.mysql.com/doc/refman/5.7/en/innodb-redo-log.html https://dev.mysql.com/doc/refman/5.7/ ...
- Linux下Ngnix及PHP重启命令
INT, TERM 立刻终止 QUIT 平滑终止 USR1 重新打开日志文件 USR2 平滑重载所有worker进程并重新载入配置和二进制模块 php-fpm 关闭: kill -INT `cat / ...
- POCO c++ 使用例子
.定时器 #include "Poco/Timer.h" #include "Poco/Thread.h" using Poco::Timer; using P ...
- MySQL中InnoDB脏页刷新机制Checkpoint
我们知道InnoDB采用Write Ahead Log策略来防止宕机数据丢失,即事务提交时,先写重做日志,再修改内存数据页,这样就产生了脏页.既然有重做日志保证数据持久性,查询时也可以直接从缓冲池页中 ...