HDU1423:Greatest Common Increasing Subsequence
浅谈\(DP\):https://www.cnblogs.com/AKMer/p/10437525.html
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1423
最长上升子序列:https://www.cnblogs.com/AKMer/p/10437536.html
最长公共子序列:https://www.cnblogs.com/AKMer/p/10437542.html
由于此题求的是最长公共上升子序列,所以我们需要在最长公共子序列的\(n^2dp\)状态再加一个关于权值的维度。
设\(f[i][j][k]\)表示\(a\)的\([1,i]\)与\(b\)的\([1,j]\)的以权值\(k\)结束的最长公共上升子序列是多少。
若\(a[i]!=b[j]\),\(f[i][j][k]=max(f[i-1][j][k],f[i][j-1][k])\)
若\(a[i]==b[j]\),\(f[i][j][k]=max(f[i-1][j][k],f[i][j-1][k],f[i-1][j-1][x]+1)(0\leqslant x<k)\)
时间复杂度:\(O(n^3)\)
空间复杂度:\(O(n^3)\)
但是出题人似乎并不会给一个\(G\)让我们这样搞,所以这种做法因为空间复杂度不过关而以失败告终。
所以我们可以试着消去第三维,把第三维和第二维结合在一起。
令\(f[i][j]\)表示\(a\)序列的区间\([1,i]\)与\(b\)序列的区间\([1,j]\)的以\(b[j]\)结尾的最长公共上升子序列。
若\(a[i]!=b[j]\),\(f[i][j]=f[i-1][j]\)
若\(a[i]==b[j]\),\(f[i][j]=f[i-1][k]+1(0\leqslant k <j,b[k]<b[j])\)
时间复杂度:\(O(n^3)\)
空间复杂度:\(O(n^2)\)
代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=505;
int n,m;
int f[maxn][maxn];
int a[maxn],b[maxn];
int read() {
int x=0,f=1;char ch=getchar();
for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1;
for(;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-'0';
return x*f;
}
int main() {
int T=read();
while(T--) {
memset(f,0,sizeof(f));
n=read();
for(int i=1;i<=n;i++)
a[i]=read();
m=read();
for(int i=1;i<=m;i++)
b[i]=read();
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
if(a[i]!=b[j])f[i][j]=f[i-1][j];
else {
for(int k=0;k<j;k++)
if(b[k]<b[j])f[i][j]=max(f[i-1][k]+1,f[i][j]);
}
int ans=0;
for(int i=1;i<=m;i++)
ans=max(ans,f[n][i]);
printf("%d\n",ans);
if(T)puts("");
}
return 0;
}
其实当\(a[i]==b[j]\)的时候我们要枚举的\(k\)可以在之前的计算中用一个变量\(mx\)存下来,然后\(O(1)\)更新。
时间复杂度:\(O(n^2)\)
空间复杂度:\(O(n^2)\)
代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=505;
int n,m;
int f[maxn][maxn];
int a[maxn],b[maxn];
int read() {
int x=0,f=1;char ch=getchar();
for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1;
for(;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-'0';
return x*f;
}
int main() {
int T=read();
while(T--) {
memset(f,0,sizeof(f));
n=read();
for(int i=1;i<=n;i++)
a[i]=read();
m=read();
for(int i=1;i<=m;i++)
b[i]=read();
for(int i=1;i<=n;i++) {
int mx=0;
for(int j=1;j<=m;j++)
if(a[i]!=b[j]) {
f[i][j]=f[i-1][j];
if(b[j]<a[i])mx=max(mx,f[i-1][j]);
}
else f[i][j]=mx+1;
}
int ans=0;
for(int i=1;i<=m;i++)
ans=max(ans,f[n][i]);
printf("%d\n",ans);
if(T)puts("");
}
return 0;
}
HDU1423:Greatest Common Increasing Subsequence的更多相关文章
- HDU1423:Greatest Common Increasing Subsequence(LICS)
Problem Description This is a problem from ZOJ 2432.To make it easyer,you just need output the lengt ...
- Greatest Common Increasing Subsequence hdu1423
Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536 ...
- HDU 1423 Greatest Common Increasing Subsequence LCIS
题目链接: 题目 Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...
- POJ 2127 Greatest Common Increasing Subsequence -- 动态规划
题目地址:http://poj.org/problem?id=2127 Description You are given two sequences of integer numbers. Writ ...
- HDOJ 1423 Greatest Common Increasing Subsequence -- 动态规划
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1423 Problem Description This is a problem from ZOJ 2 ...
- ZOJ 2432 Greatest Common Increasing Subsequence(最长公共上升子序列+路径打印)
Greatest Common Increasing Subsequence 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problem ...
- POJ 2127 Greatest Common Increasing Subsequence
You are given two sequences of integer numbers. Write a program to determine their common increasing ...
- HDUOJ ---1423 Greatest Common Increasing Subsequence(LCS)
Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536 ...
- 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 ...
随机推荐
- 20145210姚思羽《网络对抗》MSF基础应用实验
20145210姚思羽<网络对抗>MSF基础应用实验 实验后回答问题 1.用自己的话解释什么是exploit,payload,encode. exploit就是进行攻击的那一步 paylo ...
- 20145240 《Java程序设计》第十周学习总结
20145240 <Java程序设计>第十周学习总结 教材学习内容总结 网络编程 网络编程就是在两个或两个以上的设备(例如计算机)之间传输数据. 程序员所作的事情就是把数据发送到指定的位置 ...
- collectionView的案例
#import "ViewController.h" #import "CollectionViewCell.h" @interface ViewControl ...
- tcp底层连接过程(c语言)
在用了多种上位机开发环境,包括mfc.Qt.C#之后,发现它们的API都是对底层协议的(可以说是C语言)的封装,所以了解了底层协议,任意换上位机开发环境都是没问题的. 1.服务器创建套接字socket ...
- Python 字典Dict概念和操作
# 字典概念:无序的, 可变的键值对集合 # 定义 # 方式1 # {key: value, key: value...} # 例如 # {"name": "xin&qu ...
- mysql服务器3306端口不能远程连接的解决
1.网络检测 1)ping主机可以: 2)telnet 主机3306端口不可以: telnet 主机22端口可以: 说明与本机网络没有关系: 2.端口检测 1)netstat ...
- spring+springmvc+mybatis(ssm)
1.jdbc.properties jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/jk ...
- 【转】meta标签中的http-equiv属性使用介绍
meta是html语言head区的一个辅助性标签.也许你认为这些代码可有可无.其实如果你能够用好meta标签,会给你带来意想不到的效果,meta标签的作用有:搜索引擎优化(SEO),定义页面使用语言, ...
- ajax01简介
(Asynchronous JavaScript and XML)Ajax :异步 JavaScript 和 XML,一种允许浏览器和服务器通信进行少量数据交换而无需重新加载整个网页,以实现更新部分网 ...
- HTTP状态码 304
HTTP 304 错误Not Modified 客户端有缓冲的文档并发出了一个条件性的请求(一般是提供If-Modified-Since头表示客户只想比指定日期更新的文档).服务器告诉客户,原来缓冲的 ...