HDU 1423 LICS 模板
http://acm.hdu.edu.cn/showproblem.php?pid=1423
4、LICS、O(lena * lenb)
设dp[i][j]表示a[]的前i项,以b[]的第j项结尾时,能匹配的最大值。
①、不匹配a[i]这个数,则是dp[i][j] = dp[i – 1][j];
②、匹配a[i]这个数,则需要a[i] == b[j] && b[j] > b[k] 推出 dp[i][j] = max(dp[i – 1][k]) + 1,
这样复杂度需要O(n3),注意到,求解dp的时候,是从dp[i][1….lenb]这样的顺序求解,而且,需要a[i] == b[j]才能算做贡献,因为要LCS嘛!那么可以记录dp[i][1…j – 1]的信息,以a[i]作为基准(因为a[i] == b[j]才能算出贡献,以那个作为基准没所谓),找出前j - 1个数中,满足LIS并且最大的那个,O(1)更新即可。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
const int maxn = + ;
int dp[maxn][maxn];
int a[maxn], b[maxn];
void work() {
int lena, lenb;
cin >> lena;
for (int i = ; i <= lena; ++i) {
cin >> a[i];
}
cin >> lenb;
for (int i = ; i <= lenb; ++i) {
cin >> b[i];
}
memset(dp, , sizeof dp);
for (int i = ; i <= lena; ++i) {
for (int j = , cnt = ; j <= lenb; ++j) {
dp[i][j] = dp[i - ][j]; //不要当前这个a[i]
if (a[i] > b[j]) { //形成LIS
cnt = max(cnt, dp[i - ][j]);
}
if (a[i] == b[j]) { //形成LCS
dp[i][j] = cnt + ;
}
}
} int ans = ;
for (int i = ; i <= lenb; ++i) ans = max(ans, dp[lena][i]);
cout << ans << endl;
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
int t;
cin >> t;
while (t--) {
work();
if (t)
printf("\n");
}
return ;
}
HDU 1423 LICS 模板的更多相关文章
- hdu 1686 KMP模板
// hdu 1686 KMP模板 // 没啥好说的,KMP裸题,这里是MP模板 #include <cstdio> #include <iostream> #include ...
- HDU 1423 Greatest Common Increasing Subsequence(最长公共上升LCIS)
HDU 1423 Greatest Common Increasing Subsequence(最长公共上升LCIS) http://acm.hdu.edu.cn/showproblem.php?pi ...
- HDU 2138 Miller-Rabin 模板题
求素数个数. /** @Date : 2017-09-18 23:05:15 * @FileName: HDU 2138 miller-rabin 模板.cpp * @Platform: Window ...
- HDU 1423 Greatest Common Increasing Subsequence(LICS入门,只要求出最长数)
Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536 ...
- hdu 1423(LCS+LIS)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1423 好坑啊..还有公共串为0时的特殊判断,还有格式错误..看Discuss看知道除了最后一组测试数据 ...
- HDU 1423 最长公共字串+上升子序列
http://acm.hdu.edu.cn/showproblem.php?pid=1423 在前一道题的基础上多了一次筛选 要选出一个最长的递增数列 lower_bound()函数很好用,二分搜索找 ...
- hdu 1348 凸包模板
http://acm.hdu.edu.cn/showproblem.php?pid=1348 造城墙问题,求出凸包加上一圈圆的周长即可 凸包模板题 #include <cstdio> #i ...
- HDU 1392 凸包模板题,求凸包周长
1.HDU 1392 Surround the Trees 2.题意:就是求凸包周长 3.总结:第一次做计算几何,没办法,还是看了大牛的博客 #include<iostream> #inc ...
- HDU 2586 (LCA模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2586 题目大意:在一个无向树上,求一条链权和. 解题思路: 0 | 1 / \ 2 3 ...
随机推荐
- JVM手动分配内存(转载)
原文内容很详细,不利于快速浏览,所以只保留了重点 原文地址 http://blog.csdn.net/mr__fang/article/details/47723767 内存检测工具jvisualVM ...
- OO的片段,继承与组合,继承的优点与目的,虚机制在构造函数中不工作
摘自C++编程思想: ------------------------------ 继承与组合:接口的重用 ------------------------------- 继承和组合都允许由已存在的类 ...
- Spring的声明式事务
1.与hibernate集成 <bean id="sessionFactory" class="org.springframework.orm.hibernate3 ...
- Java面试手写代码题
1.栈实现 2.Iterator实现 3.单例 4.多线和控制(暂停,恢复,停止) 5.生产者消费者
- lstat函数的使用【学习笔记】
通过lstat函数获取文件的类型的代码如下. #include "apue.h" int main(int argc,char *argv[]) { int i; struct s ...
- 「咕咕网校 - 基础省选」树上问题的进阶 by Drench
一定要在noip之前把自己花钱买的Luogu网课梳理完!QAQ 树上前缀和: 对于有根树,在每个点记录 val (点权) 和 sum(到根的点权之和) 当然记录的值因题而异(但是既然叫树上前缀和当然就 ...
- PostgreSQ 连接问题 FATAL: no pg_hba.conf entry for host
PostgreSQ数据库为了安全,它不会监听除本地以外的所有连接请求,当用户通过JDBC访问是,会报一些如下的异常: org.postgresql.util.PSQLException: FATAL: ...
- 【转】nose-parameterized是Python单元测试框架实现参数化的扩展
原文地址: http://www.cnblogs.com/fnng/p/6580636.html 相对而言,Python下面单元测试框架要弱上少,尤其是Python自带的unittest测试框架,不支 ...
- bzoj2839
容斥原理+组合数学 看见这种恰有k个的题一般都是容斥原理,因为恰有的限制比较强,一般需要复杂度较高的方法枚举,而容斥就是转化为至少有k个,然后通过容斥原理解决 我们先选出k个元素作为交集,有C(n,k ...
- In-App Purchase Programming Guide----(二) ---- Designing Your App’s Products
Designing Your App’s Products A product is something you want to sell in your app’s store. You creat ...