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 ...
随机推荐
- SpringBoot 系列
https://my.oschina.net/xiedeshou?tab=newest&catalogId=5936801 SpringBoot | 第零章:前言 SpringBoot | 第 ...
- December 28th, Week 52nd Saturday, 2019
If you start at the bottom, pay your dues, life here can be a dream come true. 只要你从头开始,脚踏实地,梦想是可以成真的 ...
- 让一个div层于窗口中间位置
这几天写的前端代码比较多,为了体验更好,有时会让div弹层位于窗口中间,在百度找到这样一个答案: <style type="text/css"> #box{ posti ...
- 使刚编辑的vim编辑器配置文件立即生效(实为自动生效)
简单的说,在虚拟机下安装的Centos6.3系统后,默认的是没有vim编辑器的配置文件,此时如果有必要,可以按照自己的习惯定制或配置自己的vim编辑器: 1,如果你是root权限,进入root目录下: ...
- jQuery结合CSS实现手风琴组件
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- Java IO流详解(四)——字符流Reader和Writer
前面一章介绍了字节流的使用,提到了字节流在处理utf-8编码的中文可能会出现乱码的情况(其他编码的中文同样会出现乱码),所以Java针对这一情况提供了字符流. 但是字符流只能处理字符,不能用来处理 . ...
- Bcrypt加密算法简介
用户表的密码通常使用MD5等不可逆算法加密后存储,为防止彩虹表破解更会先使用一个特定的字符串(如域名)加密,然后再使用一个随机的salt(盐值)加密. 特定字符串是程序代码中固定的,salt是每个密码 ...
- javascript入门教程02
JavaScript中的运算符 (1)算术运算符 + :相加 var a=123,b=45; document.write(a+b); - :相减 document.write(a-b); *:相乘 ...
- vim功能之替换和查找
vim有着强大的替换和查找功能,若能进行熟练的运用,可以让工作效率得到一个很大程度的提高. 替换 语法:[addr]s/源字符串/目的字符串/[option] [addr]表示检索范围,如: &quo ...
- SpringBoot与Mybatis-plus整合,代码生成mvc层
一.添加pom依赖 <!-- mysql驱动 --> <dependency> <groupId>mysql</groupId> <artifac ...