HDU5904 思路不错的dp....比我的蠢逼LCS强多了

LCS, LIS, 线性空间LCS( hirschberg算法)  今天帮同学写了三个代码,分别是简单LCS,LIS,以及使用线性空间的LCS。首先是LCS(longest common substring最长公共子序列),这是动态规划的典型例题,各种算法书里都会讲到的一个问题。个人推荐这个网页http://blog.163.com/hzzy-010/blog/static/79692381200872024242126/ 。 讲的非常清楚,图例也很好,可以 阅读详情

Problem Description

Alex has two sequences a1,a2,…,an and b1,b2,…,bm. He wants find a longest common subsequence that consists of consecutive values in increasing order.

Input

There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:

The first line contains two integers n and m (1≤n,m≤100000) – the length of two sequences. The second line contains n integers: a1,a2,…,an (1≤ai≤106). The third line contains n integers: b1,b2,…,bm (1≤bi≤106).

There are at most 1000 test cases and the sum of n and m does not exceed 2×106.

Output

For each test case, output the length of longest common subsequence that consists of consecutive values in increasing order.

Sample Input

3
3 3
1 2 3
3 2 1
10 5
1 23 2 32 4 3 4 5 6 1
1 2 3 4 5
1 1
2
1

Sample Output

1
5
0

这题的关键点被我这个蠢逼给漏了…
那就是递增的差必须为1…
这样只要两个数列挨个扫一下就可以的。
然而一看见最长公共我就脑子发热写了个滚动的LCS
宛如一个智障….n+m=2*10^6讲道理mn对半分的话….
滚动数组省空间但是不省时间啊
10^12….gg

看了dalao的代码豁然开朗
不管有没有xjb转移就对了….
这样没有就是一转移…
简直完美…

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<memory.h>
using namespace std;
int n, m;
int dp1[200001], dp2[200001],a[100001],b[100001];
int main()
{
    int T;
    cin >> T;
    while (T--)
    {
        cin >> n >> m;
        memset(dp1, 0, sizeof(dp1));
        memset(dp2, 0, sizeof(dp2));
        for (int q = 0;q < n;q++)scanf("%d", &a[q]);
        for (int q = 0;q < m;q++)scanf("%d", &b[q]);
        for (int q = 0;q < n;q++)dp1[a[q]] = dp1[a[q] - 1] + 1;
        for (int q = 0;q < m;q++)dp2[b[q]] = dp2[b[q] - 1] + 1;
        int sum = 0;
        for (int q = 0;q < 200001;q++)sum = max(sum, min(dp1[q], dp2[q]));
        cout << sum << endl;
    }
    return 0;
}
【26美赛A题】2026美赛数学建模(MCM/ICM)思路解析及代码分享 本文介绍了2026年美赛数学建模A题的解题思路与方法。A题通常属于连续型系统问题,重点在于构建可解释的机理模型,通过将现实机制转化为动态方程进行仿真预测。文章提出了通用建模路线,包括机理抽象、模型假设、动态建模、参数识别、系统仿真和情景预测六个阶段。专栏将提供从机理到方程的系统建模思路、参数标定方法、Python/Matlab实现代码以及高分论文模板。内容适合参加2026美赛A题、偏重物理/工程建模方向的参赛队伍,旨在提升机理建模能力。后续将更新完整题解和源码。 阅读详情

相关推荐

《Hadoop大数据技术原理与应用》(第2版)黑马程序员的课后习题答案

《Hadoop大数据技术原理与应用》(第2版)黑马程序员的课后习题答案

qq_36773158的博客 4686

Hirschberg 's method for LCS

一种采用了分治思想和动态规划的LCS算法,仅需O(NM)时间复杂度和O(min(N,M))空间复杂度就可得最大子串及其长度,具体参见论文:A Linear Space Algorithm for Computing Maximal Common Subsequences。这里介绍一下自己的理解。 如何分治? 给定两个字符串X(长度为m),Y(长度为n)。将X平分为两个子串X(1,m/2

trash 2260

ARM架构下Horizon Client安装全攻略:从依赖检查到环境变量配置

本文详细介绍了在ARM架构设备上安装VMware Horizon Client的全过程,包括依赖检查、软件包获取、系统部署和环境变量配置。特别针对华为鲲鹏等ARM平台,提供了从基础准备到性能优化的完整指南,帮助用户高效完成虚拟桌面客户端的部署与调优。

github5actions的博客 568

M斐波那契数列 HDU - 4549(杜教bm+欧拉降幂+快速幂)

题意: M斐波那契数列F[n]是一种整数数列,它的定义如下: F[0] = a F[1] = b F[n] = F[n-1] * F[n-2] ( n &amp;gt; 1 ) 现在给出a, b, n,你能求出F[n]的值吗? 分析: f[0]=a; f[1]=b; f[2]=a * b; f[3]=a * b^2; f[4]=a^2 * b^3; f[5]=a^3 * b^5; … f[n]=a^fib...

weixin_43541529的博客 402

编程_三大编程思想:POP、OOP、AOP

简单介绍POP、OOP、AOP三大编程思想的区别和优缺点~

mikimo的博客 3909

ST-LINK调试原理与STM32硬件连接实战指南

嵌入式调试器是连接开发环境与物理MCU的核心枢纽,其本质是基于ARM CoreSight架构的实时硬件观测与控制接口。它通过JTAG或SWD协议访问内核寄存器、内存及外设,实现断点、单步、内存监视等深度调试能力,远超简单程序烧录功能。ST-LINK作为主流ARM Cortex-M调试器,支持SWD(2线)与JTAG(多线)双模式,在电平匹配、复位控制、引脚复用规避等硬件层面临关键工程约束。掌握VREF参考电压配置、nSRST硬复位接线、调试引脚隔离策略及Keil MDK调试会话配置,是保障STM32稳定联机

weixin_42596214的博客 318

Happy Necklace hdu 6030 (BM算法)

Little Q wants to buy a necklace for his girlfriend. Necklaces are single strings composed of multiple red and blue beads. Little Q desperately wants to impress his girlfriend, he knows that she will ...

dsaghjkye的博客 326

TMC2209的UART模式详解:如何用串口调试助手配置细分和电流(避坑指南)

本文深入解析TMC2209步进电机驱动器的UART模式,详细讲解如何通过串口调试助手配置细分和电流,包括硬件连接、通信协议、关键寄存器设置及高级功能应用。提供实战避坑指南,帮助开发者优化步进电机控制性能,适用于精密仪器、3D打印等场景。

weixin_29166013的博客 551

HDU 5904 - LCIS

Problem Description Alex has two sequences a1,a2,...,an and b1,b2,...,bm. He wants find a longest common subsequence that consists of consecutive values in increasing order. Input There are mul

Shiina Mashiro no youni 602

Frida实战:高效dump Android内存中的so文件与修复技巧

本文详细介绍了使用Frida工具dump Android内存中已解密so文件的实战技巧。针对加固应用静态分析失效的痛点,文章提供了从环境搭建、使用frida_dump脚本自动化抓取内存数据,到利用SoFixer工具修复ELF文件结构的完整流程,帮助逆向分析人员高效获取可分析的so文件。

tt34567的博客 543

MinerU在线API异步调用代码开发示例

本文展示了如何使用Python异步编程调用MinerU在线API进行文件批量上传。代码示例包含三个主要功能:1) 通过API密钥获取文件上传URL;2) 使用信号量控制并发上传多个文件;3) 获取提取结果。实现采用httpx异步客户端和asyncio库,支持文件存在性检查、错误处理和日志记录。示例中演示了如何上传PDF文件到MinerU平台,并提供了获取批量处理结果的接口。该代码可作为异步调用MinerU API的参考实现。

拥抱具身智能,昇腾Ascend C 372

D - Covering HDU - 6185 (杜教板子(BM)线性递推式)

D - Covering HDU - 6185 Bob's school has a big playground, boys and girls always play games here after school. To protect boys and girls from getting hurt when playing happily on the playground, ...

nucleare的博客 361

apple游客下单逆向分析

Apple游客下单Cookies注册激活流程逆向分析与算法实现

吴秋霖的博客 2013

HDU3493 The Little Architect 【BM算法推公式+矩阵快速幂】

题目链接:点击打开链接 首先先考虑dp解法。设dp[i][j]为当前使用了i个块,最下一层的块数为j块的时候的方案数。 于是可以得到: ·dp[i][i]=1 ·dp[i][j]=dp[i-j][k]*(k-j+1)    (1j) 于是答案为ans[n]=dp[n][i]   (1 时间复杂度为O(n^3),显然超时。 不过可以利用dp的方式打出前几项的表: 1,2,6,19,6

Code by Caproner 729

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

Codeforces Round #479 F. Consecutive Subsequence(DP)

You are given an integer array of length nn.You have to choose some subsequence of this array of maximum length such that this subsequence forms a increasing sequence of consecutive integers. In other...

SSimpLe_Y的博客 800

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

Hirschberg的LCS算法实现

解决Longest Common Subsequence(LCS)问题最常用的算法是Dyanmic programing,细节可以参考Ch15.4 of Introduction of Algorithm(2ED), MIT press, p 350。这个算法最大的问题是他的空间复杂度是O(m*n)。这样,当两个序列达到上万个节点时,内存消耗就成为了大问题。1975年D. S. Hirsc

技术随笔 9168

hdu 5904 水题

链接:戳这里 LCIS Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem Description Alex has two sequences a1,a2,...,an and b1,b2,...,bm. He w

Cross 792

动态规划解决最长公共子序列(Longest Common Subsequence, LCS)问题

目录 1 LCS定义 2 LCS的算法 3 LCS的算法的证明 4 LCS伪代码 5倒推法重构LCS 6 动态规划的特点 1 LCS定义 假设有两个序列 x[1,..., m] 和 y[1,...,n]分别为 x: A B C B D A B y: B D C A B A 很容易观察出X,Y的最长子序列为:BDAB,BCAB,BCBA。这里要说的是,子序列只要字符在原始序...

学如不及,犹恐失之 929

思维 hdu 5904 (LCIS)

LCIS Problem Description Alex has two sequences a1,a2,...,an and b1,b2,...,bm. He wants find a longest common subsequence that consists of consecutive values in increasing order.  

渣渣眼里没有水题 372

hdu 5904 LCIS【dp】

LCIS Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 649    Accepted Submission(s): 303 Problem Description Alex has two sequences a1,a

mengxiang000000 794

hdu5950(BM递推)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5950 思路:直接套板子啊,不过要改一下 #include &lt;cstdio&gt; #include &lt;cstdlib&gt; #include &lt;cassert&gt; #include &lt;cstring&gt; #include &lt;bitset&gt; #incl...

llmxby 511

lightoj 1031 被区间dp虐哭...看题解才学会

You are playing a two player game. Initially there are n integer numbers in an array and player A and B get chance to take them alternatively. Each player can take one or more numbers from the left or

NineFailure的博客 2840

HDU1024 这压缩真先进...先进的我想跳河

DescriptionNow I think you have got an AC in Ignatius.L’s “Max Sum” problem. To be a brave ACMer, we always challenge ourselves to more difficult problems. Now you are faced with a more difficult probl

NineFailure的博客 1509

POJ1661 神坑卧槽!!

Description“Help Jimmy” 是在下图所示的场景上完成的游戏。 场景中包括多个长度和高度各不相同的平台。地面是最低的平台,高度为零,长度无限。 Jimmy老鼠在时刻0从高于所有平台的某处开始下落,它的下落速度始终为1米/秒。当Jimmy落到某个平台上时,游戏者选择让它向左还是向右跑,它跑动的速度也是1米/秒。当Jimmy跑到平台的边缘时,开始继续下落。Jimmy每次下落的高度不能超

NineFailure的博客 1318

HDU1069终于会一个不算水的DP

Problem Description A group of researchers are designing an experiment to test the IQ of a monkey. They will hang a banana at the roof of a building, and at the mean time, provide the monkey with some

NineFailure的博客 1019

HDU1159最长公共子序列

这个题貌似是算法导论的原题 不过本着能循环坚决不用递归的态度 我认真地写了循环… 关键其实就是一个转移方程 如果两个字节一样,他就等于 dp[a][b]=dp[a+1][b+1]+1; 如果不一样就找两边最大的 dp[a][b]=max(dp[a][b+1],dp[a+1][b]); 另外在DP题里有点找到感觉了,如果想要用循环就先找到以前递归的感觉,然后自底向上加回去#includ

NineFailure的博客 773

codeforces768D(87/600)

D. Jon and Orbs time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output Jon Snow is on the lookout for some orbs required to defeat the white walk

NineFailure的博客 772
上一篇: lightoj 1008 找找规律...水
下一篇: poj2955 水区间DP
NineFailure
博客等级 码龄11年 22粉丝 317原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址