HDU1841 Find the Shortest Common Superstring(两次KMP)

『杭电1841』Find the Shortest Common Superstring Problem Description The shortest common superstring of 2 strings S1and S2is a string S with the minimum number of characters which contains both S1and S2as a sequence of consecutive characters. For instance, the shortest common superstring of “alba” ... 阅读详情

链接:

http://acm.hdu.edu.cn/showproblem.php?pid=1841

题意:

两个字符串拼接,问拼成新串的最小长度,使得新串包含两个原串。

思路:

跑两遍kmp,找最长前缀。(先排除包含情况)

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6+10;
int next[maxn];
void getnext(char *b)
{
    int len = strlen(b+1);
    memset(next,0,sizeof next);
    int j=0;
    for(int i=2;i<=len;i++)
    {
        while(j&&b[j+1]!=b[i]) j=next[j];
        if(b[j+1]==b[i]) j++;
        next[i]=j;
    }
}
int kmp(char *a,char *b)
{
    int l1=strlen(a+1),l2=strlen(b+1);
    getnext(b);
    int j=0;
    for(int i=1;i<=l1;i++)
    {
         while(j&&b[j+1]!=a[i])j=next[j];
         if(b[j+1]==a[i])j++;
         if(j==l2) return -1;
    }
    return j;
}
int main()
{
    int  t;
    cin>>t;
    while(t--)
    {
        char s1[maxn],s2[maxn];
        scanf("%s%s",s1+1,s2+1);
        int r=kmp(s1,s2);
        int l=kmp(s2,s1);
        if(r==-1||l==-1)
            cout<<max(strlen(s1+1),strlen(s2+1))<<endl;
        else
            cout<<strlen(s1+1)+strlen(s2+1)-max(r,l)<<endl;
    }
    return 0;
}

 

给新人的储能系统入门图解:从电芯到EMS,一张图看懂BESS里那些‘黑话’ 本文通过视觉化拆解储能系统,从电芯到EMS详细解析BESS中的关键组件和行业术语。涵盖电芯参数、模组组装、PCS功能、BMS与EMS分工,以及SOC、DOD等关键运维参数,帮助新人快速理解储能系统的核心技术和应用场景。 阅读详情

相关推荐

从零到一:用AXI DataMover在Zynq MPSoC上实现高效DMA传输(附Vitis HLS驱动示例)

本文详细介绍了如何在Xilinx Zynq UltraScale+ MPSoC平台上使用AXI DataMover实现高效的DMA数据传输。通过解析AXI DataMover架构、Vivado工程配置、Vitis HLS驱动开发及性能优化技巧,帮助开发者快速构建高性能数据传输通道,并提供了可直接复用的代码示例。

weixin_42520796的博客 430

【项目实训日志二】Shortest Superstring最短超串问题

Shortest Superstring最短超串问题问题定义定义overlap贪心算法举例A factor 4 algorithm一个近似比为4的算法具体算法理解算法证明近似比为4引理7.2引理7.3定理7.4证明把近似比优化至3定义压缩具体算法证明近似比为3引理7.6引理7.7 本文章总结一下《Approxiamte Algorithms》一书中第七章Shortest Superstring的相关内容。 问题定义 Given a finite alphabet Σ, and a set of n stri

qq_41910471的博客 1368

CentOS8安装NVIDIA显卡驱动、CUDA和Anaconda

CentOS8安装NVIDIA显卡驱动和CUDA 参考:CentOS nvidia+cuda+cudnn 安装 安装英伟达显卡驱动 1、检查是否安装了NVIDIA的GPU(硬件层面): lspci | grep -i nvidia 2、安装GCC、kernal组件、dkms等 sudo yum install gcc sudo yum install gcc-c++ sudo yum -y install kernel-devel sudo yum -y install kernel-headers s

智慧的旋风的博客 1万+

【Leetcode】943. Find the Shortest Superstring

是已经选择的单词的所有下标的集合的二进制压缩(显然同一个单词不会选择超过。的前缀的最长重合的长度(这个长度我们要预处理一下)。中单词各不相同,并且没有一个单词是另一个单词的子串。可以递推出什么样的状态,递推的前提是。次,这样没有意义)。里每个单词都是其子串。),那么长度的增加值实际上是。结尾的情况下所能得到的最短。是已经选择的单词的状态为。再由这个答案倒过来推出。,每个单词长度不超过。为单词最长长度,空间。

数学、算法爱好者的博客 338

CSV文件输入到simulink中

将示波器波形和数据CSV文件保存下来进行数据分析。将波形绘制到simulink中

yay0923的博客 5329

Netapp FAS2750更换磁盘后不同步问题处理

netapp fas2750磁盘更换后不同步问题处理

jetliu05的博客 1258

leetcode942-Find the Shortest Superstring

这道题目很明显我们可以记录一个最小值和最大值,当s[i] == ‘I’ 的时候perm[i]比perm[i+1]小1,那么perm[i]可以是最小值,perm[i+1]比他大即可。当s[i]=='D’的时候perm[i]可以是最大值,perm[i+1]比他小即可,每遍历完一次就更新这个最小值和最大值,但是这种思路把问题复杂化了,这种思路每遍历一个元素要求同时更新perm[i]和perm[i+1],但是我们怎么能重复更新元素呢,而且也不能确定下一个元素到底是I还是D。输出:[0,4,1,3,2]

我的程序人生 521

不用FFmpeg!纯前端实现RTSP直播的3种现代方案对比(WebSocket/Wasm/WebCodecs)

本文深入探讨了纯前端实现RTSP直播的三种现代技术方案:WebSocket代理转发、Wasm解码和WebCodecs硬解。通过详细的架构设计、性能实测数据和兼容性分析,帮助开发者在安防监控、智慧城市等场景中选择最佳方案,实现低延迟、高效率的视频流处理。

lilh34434的博客 233

LeetCode每日一题(943. Find the Shortest Superstring)

看得官方的解法, 不想多说了, 有兴趣的看。

wangjun861205的博客 1383

【002】C++ 语言核心:关键字深度解析与分类指南

auto:自动类型推导bool:布尔类型break:跳出循环或switch语句case:switch语句分支char:字符类型class:定义类const:常量continue:结束当前循环,开始下一次循环default:switch语句默认分支delete:删除对象do:do-while循环double:双精度浮点数类型else:if语句的否定分支enum:枚举类型explicit:显式构造函数export:导出符号extern:声明外部变量或函数。

Lion_Long的博客 1252

42、Windows Server Update Services(WSUS)全面指南

本文全面介绍了Windows Server Update Services(WSUS)的安装、配置、管理与优化策略。内容涵盖WSUS基础认知、客户端更新问题解决、多服务器架构设计、计算机分组策略、更新审批与报告查看,并深入探讨了SSL加密配置、性能优化、与其他系统(如AD和SCCM)集成以及常见故障排查方法。通过本指南,IT管理员可高效部署和管理企业级软件更新,提升安全性和运维效率。

cicd6pipeline的博客 102

HDU - 1841 Find the Shortest Common Superstring

Find the Shortest Common Superstring The shortest common superstring of 2 strings S 1 and S 2 is a string S with the minimum number of characters which contains both S 1 and S 2 as a sequence of

什么样的小孩 的博客 333

刷机维修进阶教程-----oppo机型 一加 7pro 修复基带 更改参数 实例详细步骤解析

通过课程基本可以了解 1-----oppo机型与一加7机型更改参数的具体步骤解析 2------了解oppo机型开启diag端口的具体步骤 3-----了解一加7这些机型改参数常识 。可以借鉴参考用于其他机型 4-----了解手机基带分区与基带辅助分区的作用 5-----机型参数能读取到 但不能写这种情况如何操作

小马哥的专栏 1486

hdu1841 Find the Shortest Common Superstring

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1841 题解:1.s1是s2的子串,或者s2是s1的子串。2.s1是s2的前缀或者后缀。 #include #include #define MAXN 1000002 int next[MAXN]; char str1[MAXN],str2

不可不戒 847

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

Find the Shortest Common Superstring(hdu1841,KMP)

http://acm.hdu.edu.cn/showproblem.php?pid=1841 解析: 题意: 找到一个最短的串s,含有子串s1,s2 思路:两次KMP匹配 求出,s1后缀和s2前缀的最长公共串长度m1,       s2后缀和s1前缀的最长公共串长度m2;       ans=strlen(s1)+strlen(s2)-max(m1,m2); 6096 KB 359

JHC23的专栏 949

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

hdu1841 Find the Shortest Common Superstring-----KMP

Find the Shortest Common Superstring Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 971 Accepted Submission(s): 243 Problem Description T

Shirley 1430

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

hdu1841 Find the Shortest Common Superstring 两遍KMP

hdu1841 Find the Shortest Common Superstring 两遍KMP问题描述 :The shortest common superstring of 2 strings S1 and S2 is a string S with the minimum number of characters which contains both S1 and S2 as a seq

愿赌服输 703

HDU 1841 Find the Shortest Common Superstring KMP

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1841 题意:给你两个字符串,求解一个最短的字符串,两个字符串连接的时候相同的可以重叠 例如: “alba” 和“bacau”重叠在一起的最短串是 “albacau”。 我感觉这道题用KMP,求next数组就行,先将两个字符串连接起来(两种方法),然后求解next[len],当这个

lamborghini1993 1241

hdu 1841 Find the Shortest Common Superstring

hdu 1841 点击打开链接 题意:给出两个字符串s1,s2,找到一个最短的字符串s,似的s1是它的子串,s2也是它的字串。 方法:用KMP,首先判断s1是否是s2的字串,若是,则直接输出s2的长度,,即s2就是我们找的s字符串。同理判断s2是否是s1 的字串。 若都不是,,则判断s1 的前缀与s2的后缀最大重合部分k1,s2的前缀与s1后缀最大重合部分k2,判断k1与k2的大小,

ACM_korun 884

HDU 1841 Find the Shortest Common Superstring(KMP)

Find the Shortest Common Superstring Problem Description The shortest common superstring of 2 strings S1 and S2 is a string S with the minimum number of characters which contains both S1 and S2 as

持之以恒 471

hdu1841Find the Shortest Common Superstring

Find the Shortest Common SuperstringTime Limit: 1000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 2062    Accepted Submission(s): 570Problem DescriptionThe sh...

mayuqing98的博客 428

HDU 1841 Find the Shortest Common Superstring(KMP灵活运用)

ind the Shortest Common Superstring Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 1149    Accepted Submission(s): 289 Problem Descri

布生气 1365

HDU1841 Find the Shortest Common Superstring (KMP两字符串的首尾连接)

题意:给我们两个字符串,求解一个最短的字符串,两个字符串连接的时候相同的可以 重叠,例如:“alba” 和“bacau”重叠在一起的最短串是 “albacau”。 解析:最初看到这题时,是将两个串连接起来,用strcat函数,然后利用kmp求它的 循环子段,这就是我的数组为啥开的两倍,但后来发现对于“abaa”“a”或者“aaaba” “aa”….类似这一类,它的处理就会出错,后来将它分开处

天行健的博客 462

HDU 1841: Find the Shortest Common Superstring

!!!#include #include #include using namespace std; char a[2000005],b[2000005]; int nextd[2000010]; void get(char *ch) { int l = strlen(ch); int s = 0,t = -1; nextd[0] = -1; while(s <l) { if(t

无知的博客 609

HDU 1841 Find the Shortest Common Superstring

戳这里

WSMYE的博客 (hemiaomiao.me) 316

文章标题 HDU 1841:Find the Shortest Common Superstring(KMP)

Find the Shortest Common SuperstringDescriptionThe shortest common superstring of 2 strings S 1 and S 2 is a string S with the minimum number of characters which contains both S 1 and S 2 as a sequence

Wang_SF2015的博客 469
上一篇: Gym - 101550F - Fleecing the Raffle(暴力)
下一篇: 基姆拉尔森计算公式 推导
Healer66
博客等级 码龄9年 331粉丝 267原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址