Acwing272 最长公共上升子序列
题目大意:给定两个大小为n的数组,让你找出最长公共上升子序列的长度。
分析:这是一个比较好的dp题,LIS和LCS两大经典线性dp问题相结合,简称LCIS。
代码(O(n*n*n)写法):
#include<bits/stdc++.h>
using namespace std;
const int maxn = 3e3+;
int a[maxn],b[maxn];
int dp[maxn][maxn];
int main() {
int n;
cin >> n;
for (int i = ; i <= n; i++)
cin >> a[i];
for (int i = ; i <= n; i++)
cin >> b[i];
a[] = b[] = -0x3f3f3f3f;
for (int i = ; i <= n; i++) {
for (int j = ; j <= n; j++) {
if (a[i] == b[j]) {
for (int k = ; k < j; k++) {
if (b[k] < a[i])
dp[i][j] = max(dp[i][j], dp[i - ][k] + );
}
} else dp[i][j] = dp[i - ][j];
}
}
int ans = ;
for (int i = ; i <= n; i++) {
for (int j = ; j <= n; j++) {
ans = max(ans, dp[i][j]);
}
}
cout << ans << endl;
return ;
}
代码(O(n*n)写法):
#include<bits/stdc++.h>
using namespace std;
const int maxn = 3e3+;
int a[maxn],b[maxn];
int dp[maxn][maxn];
int main() {
int n;
cin >> n;
for (int i = ; i <= n; i++)
cin >> a[i];
for (int i = ; i <= n; i++)
cin >> b[i];
a[] = b[] = -0x3f3f3f3f;
for (int i = ; i <= n; i++) {
int val = ;
if (b[] < a[i]) val = dp[i - ][];
for (int j = ; j <= n; j++) {
if (a[i] == b[j])
dp[i][j] = val + ;
else
dp[i][j] = dp[i - ][j];
if (b[j] < a[i])
val = max(val, dp[i - ][j]);
}
}
int ans = ;
for (int i = ; i <= n; i++) {
for (int j = ; j <= n; j++)
ans = max(ans, dp[i][j]);
}
cout << ans << endl;
return ;
}
Acwing272 最长公共上升子序列的更多相关文章
- 题解【AcWing272】最长公共上升子序列
题面 一道线性 DP 好题. 设 \(dp_{i,j}\) 表示在所有 \(a_{1\dots i}\),\(b_{1\dots j}\) 的子序列中,以 \(b_j\) 结尾的最长公共上升子序列的最 ...
- 最长公共上升子序列(codevs 2185)
题目描述 Description 熊大妈的奶牛在小沐沐的熏陶下开始研究信息题目.小沐沐先让奶牛研究了最长上升子序列,再让他们研究了最长公共子序列,现在又让他们要研究最长公共上升子序列了. 小沐沐说,对 ...
- 最长公共上升子序列(LCIS)
最长公共上升子序列慕名而知是两个字符串a,b的最长公共递增序列,不一定非得是连续的.刚开始看到的时候想的是先用求最长公共子序列,然后再从其中找到最长递增子序列,可是仔细想一想觉得这样有点不妥,然后从网 ...
- ZOJ 2432 Greatest Common Increasing Subsequence(最长公共上升子序列+路径打印)
Greatest Common Increasing Subsequence 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problem ...
- POJ 2127 最长公共上升子序列
动态规划法: #include <iostream> #include <cstdio> #include <fstream> #include <algor ...
- [CodeForces10D]LCIS(最长公共上升子序列) - DP
Description 给定两个数列,求最长公共上升子序列,并输出其中一种方案. Input&Output Input 第一行一个整数n(0<n<=500),数列a的长度. 第二行 ...
- 最长递增子序列(lis)最长公共子序列(lcs) 最长公共上升子序列(lics)
lis: 复杂度nlgn #include<iostream> #include<cstdio> using namespace std; ],lis[],res=; int ...
- codevs 2185 最长公共上升子序列
题目链接: codevs 2185 最长公共上升子序列codevs 1408 最长公共子序列 题目描述 Description熊大妈的奶牛在小沐沐的熏陶下开始研究信息题目.小沐沐先让奶牛研究了最长上升 ...
- [ACM_动态规划] UVA 12511 Virus [最长公共递增子序列 LCIS 动态规划]
Virus We have a log file, which is a sequence of recorded events. Naturally, the timestamps are s ...
随机推荐
- 如何使用 Workman 做一个聊天室
一:首先,得简单说说 thinkphp+workerman 的安装. 安装 thinkphp5.1 composer create-project topthink/think=5.1.x-dev t ...
- XMPPFramework 框架
https://blog.csdn.net/qq_29846663/article/details/70170646 2017-04-14 11:37:02 于海明 阅读数 478更多 分类专栏: i ...
- 喵星之旅-狂奔的兔子-基于docker的redis分布式集群
一.docker安装(略) 二.下载redis安装包(redis-4.0.8.tar.gz) 以任何方式获取都可以.自行官网下载. 三.拉取centos7的docker镜像 命令:docker pul ...
- Linux 一些有用的能力
编程能力 Linux产生于一群真正的黑客.尽管人们习惯于认为Linus是Linux的缔造者,在linux包含的数以千计的文件中,也有一个名为Credits的文件记录了主要的Linux Hacker们的 ...
- ElementUI el-table 表格 行选择框改为单选
实现方法 首先,表格加一列 <el-table-column type="selection" width="55"></el-table-c ...
- pdf.js的使用(1) 站在巨人的肩膀上纯干货分享,没有华丽的词藻
以下是我在实际项目开发中的过程分享 前端是:vue+jsp 1.首先下载pdf.js(怎么下可以去百度),实在不会就私我,我发给你 1.1展示一哈我下载下来的pdf.js的目录结构 1.2接下来可 ...
- StringUtils中isEmpty方法和isBlank方法的区别
1.StringUtils.isEmpty没有忽略空格参数,是以是否为空和是否存在为判断依据. 示例: StringUtils.isEmpty("yyy") = false Str ...
- nikic / PHP-Parser 包的简单实用
解析PHP文件: <?php require 'vendor/autoload.php'; use PhpParser\ParserFactory; $code = file_get_conte ...
- IDEA & MAVEN配置代理(没用)
1. IDEA配置代理: 2. maven配置代理: 在maven中配置代理,主要配置编辑~/.m2/settings.xml文件的<proxies> socks5类型: <id&g ...
- 树莓派4B踩坑指南 - (10)安装坚果云(更新:暂不支持)
191209更新: 根据坚果云用户支持(helpdesk@nutstore.net)的官方回复,客户端不支持arm,所以本篇后续内容可以不用看了.. 原文如下: "您好,客户端似乎不支持ar ...