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 模板的更多相关文章

  1. hdu 1686 KMP模板

    // hdu 1686 KMP模板 // 没啥好说的,KMP裸题,这里是MP模板 #include <cstdio> #include <iostream> #include ...

  2. HDU 1423 Greatest Common Increasing Subsequence(最长公共上升LCIS)

    HDU 1423 Greatest Common Increasing Subsequence(最长公共上升LCIS) http://acm.hdu.edu.cn/showproblem.php?pi ...

  3. HDU 2138 Miller-Rabin 模板题

    求素数个数. /** @Date : 2017-09-18 23:05:15 * @FileName: HDU 2138 miller-rabin 模板.cpp * @Platform: Window ...

  4. HDU 1423 Greatest Common Increasing Subsequence(LICS入门,只要求出最长数)

    Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536 ...

  5. hdu 1423(LCS+LIS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1423 好坑啊..还有公共串为0时的特殊判断,还有格式错误..看Discuss看知道除了最后一组测试数据 ...

  6. HDU 1423 最长公共字串+上升子序列

    http://acm.hdu.edu.cn/showproblem.php?pid=1423 在前一道题的基础上多了一次筛选 要选出一个最长的递增数列 lower_bound()函数很好用,二分搜索找 ...

  7. hdu 1348 凸包模板

    http://acm.hdu.edu.cn/showproblem.php?pid=1348 造城墙问题,求出凸包加上一圈圆的周长即可 凸包模板题 #include <cstdio> #i ...

  8. HDU 1392 凸包模板题,求凸包周长

    1.HDU 1392 Surround the Trees 2.题意:就是求凸包周长 3.总结:第一次做计算几何,没办法,还是看了大牛的博客 #include<iostream> #inc ...

  9. HDU 2586 (LCA模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2586 题目大意:在一个无向树上,求一条链权和. 解题思路: 0 | 1 /   \ 2      3 ...

随机推荐

  1. log Configuration

    Log4j – Configuring Log4j 2 - Apache Log4j 2 https://logging.apache.org/log4j/2.x/manual/configurati ...

  2. UI标签库专题三:JEECG智能开发平台 FormValidation(表单提交及验证标签)

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/zhangdaiscott/article/details/28484209  自己定义弹出框提示 ...

  3. Collections工具类、Map集合、HashMap、Hashtable(十八)

    1.Map集合概述和特点 * A:Map接口概述 * 去重复, * 查看API可以知道, * 将键映射到值的对象, * 一个映射不能包含重复的键, * 每个键最多只能映射到一个值.* B:Map接口和 ...

  4. myeclipse8.6注册码

     loveyLR8ZC-855550-69545856608357821   

  5. Snowflake算法 ID生成

    Snowflake算法 ID生成 http://blog.csdn.net/w200221626/article/details/52064976 使用UUID或者GUID产生的ID没有规则 Snow ...

  6. BZOJ_1915_[Usaco2010 Open]奶牛的跳格子游戏_DP+单调队列

    BZOJ_1915_[Usaco2010 Open]奶牛的跳格子游戏_DP+单调队列 Description 奶牛们正在回味童年,玩一个类似跳格子的游戏,在这个游戏里,奶牛们在草地上画了一行N个格子, ...

  7. BZOJ_2251_[2010Beijing Wc]外星联络_后缀数组

    BZOJ_2251_[2010Beijing Wc]外星联络_后缀数组 Description 小 P 在看过电影<超时空接触>(Contact)之后被深深的打动,决心致力于寻 找外星人的 ...

  8. SpringMVC上传多文件

    springMVC实现 多文件上传的方式有两种,一种是我们经常使用的以字节流的方式进行文件上传,另外一种是使用springMVC包装好的解析器进行上传.这两种方式对于实 现多文件上传效率上却有着很大的 ...

  9. "standard,singleTop,singleTask,singleInstance"-Android启动模式

    安卓有4种启动模式,下面我们就进行详细的讲解 用栈的思维去理解,就能理解这些启动模式的本质了 先设置两个页面: A(为测试对象),B两个页面,两个页面都有跳至对方的按钮 一.标准模式(standard ...

  10. Start Developing Mac Apps -- Human Interface Design 用户界面设计

    Human Interface Design It’s not enough to create an app that works. Users expect Mac apps to be powe ...