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. HashMap随机取值和迭代器取值的对比

    一共四中方法,前两种是迭代器取值,后两种是随机取值,循环了5000万次,时间分别为:迭代器读取的速度大约是随机读取的速度的1.5倍,数据量越大,差距越明显. 另外,插入是读取的100倍左右的时间(这个 ...

  2. Session Timeout 与 $.ajaxSetup

    对于session过期跳转的问题,很简单,就是一个过滤器,然后判断session为空?跳转:继续.但是对于ajax的请求,需要做特殊处理,见下面代码中的 // 此处考虑ajax操作session过期的 ...

  3. 浅谈HTTPS连接

    相信很多朋友都遇到过网页被强插广告的情况,好端端一个干净的页面,动不动就被插了个屠龙宝刀点击就送的小窗口,看着就心烦.这种网页劫持强插广告的现象,在中国非常常见,往往是运营商进行HTTP劫持所造成的. ...

  4. 超全!整理常用的iOS第三方资源(转)

    超全!整理常用的iOS第三方资源 一:第三方插件 1:基于响应式编程思想的oc 地址:https://github.com/ReactiveCocoa/ReactiveCocoa 2:hud提示框 地 ...

  5. JVM学习资料收集

    JVM实用参数(一)JVM类型以及编译器模式 http://ifeve.com/useful-jvm-flags-part-1-jvm-types-and-compiler-modes-2/ JVM实 ...

  6. POJ2251 Dungeon Master —— BFS

    题目链接:http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total S ...

  7. JAVA MAIL基本功能

    1. [代码][Java]代码package emailrobot; import java.io.*;import java.text.*;import java.util.*;import jav ...

  8. 用于JS日期格式化,以及简单运算的Date包装工具类

    1. [文件] yDate.js/** * | yDate.js | Copyright (c) 2013 yao.yl | email: redrainyi@126.com | Date: 2012 ...

  9. Ubuntu bitnami gitlab 安装

    /************************************************************************************** * Ubuntu bit ...

  10. Android Studio 1.5.1

    Android Studio 1.5.1 December 3rd, 2015: For information on what's new in 1.5.1, see the release ann ...