hdu 1841 Find the Shortest Common Superstring

SuccessFactors入门篇 SuccessFactors入门篇—— Employee Central(一)最近通过一个月左右的学习,拿到了SF EC(SuccessFactors Employee Central )模块SAP认证,所以打算把自己学习SF的整个过程以及相关资料和大家分享一下,相互切磋,共同进步。Employee Central简单了解Employee Central简单了解新的改变功能快捷键合理的创建标题,有... 阅读详情

hdu 1841 点击打开链接


题意:给出两个字符串s1,s2,找到一个最短的字符串s,使得s1是它的子串,s2也是它的字串。

方法:用KMP,首先判断s1是否是s2的字串,若是,则直接输出s2的长度,,即s2就是我们找的s字符串。同理判断s2是否是s1 的字串。

若都不是,,则判断s1 的前缀与s2的后缀最大重合部分k1,s2的前缀与s1后缀最大重合部分k2,判断k1与k2的大小,然后用s1的长度加s2的长度减去k1,k2中较大者。

#include<iostream>
#include<cstring>
#include<algorithm>
#include<string>
#include<cstdio>
#include<cmath>
#include<cstdlib>
using namespace std;
#define maxn  1000009
char a[maxn];
char b[maxn];
int next[maxn];
void get_next(char *a)
{
    int i=0,j=-1;
    next[0] = -1;
    while( a[i] )
    {
        if(j == -1 || a[i]==a[j])
        {
            if(a[++i]!=a[++j]) next[i] = j;
            else next[i] = next[j];
        }else j = next[j];
    }
}
int KMP(char *a,char *b)///用a的前缀去匹配b的后缀
{
    int i=0,j=0,lena = strlen(a),lenb = strlen(b);
    get_next(a);
    while(j < lenb && i < lena )
    {
        if(i==-1 || a[i]==b[j]) ++i,++j;
        else i = next[i];
    }
    if(i>=lena) return -3;
    else return i;
}
int main()
{
    int T;cin>>T;
    while(T--){
        scanf("%s%s",a,b);
        int k1 = KMP(a,b);
        int k2 = KMP(b,a);
        if(k1==-3) printf("%d\n",strlen(b));
        else if(k2==-3) printf("%d\n",strlen(a));
        else {
         printf("%d\n",strlen(a)+strlen(b)-max(k1,k2));
        }
    }return 0;
}


这是我发表的第一篇KMP,因为刚学的时候自己都不怎么了解KMP,,不敢误人子弟啊。。


在Android Jetpack Compose中实现夜间模式 Android基于Compose的深色主题实现 阅读详情

相关推荐

头歌OpenGauss数据库 - 单表查询 答案

本关任务:用SELECT语句检索数据表中指定字段的数据;用SELECT语句检索数据表中所有字段的数据。为了完成本关任务,你需要掌握: 1.如何获取数据表中指定字段的数据;2.如何获取数据表中的所有数据。

d214493517的博客 5723

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

【正点原子I.MX6U-MINI移植篇】Ubuntu-base根文件系统移植构建过程详解(四)

能不能在ARM板上运行Ubuntu呢?答案肯定是可以的,Ubuntu是Linux系统的一种,可以简单的将Ubuntu理解为一个根文件系统,和我们用busybox、buildroot制作的根文件系统一样。因此移植Ubuntu也就是将Ubuntu根文件系统移植到我们的开发板上。

果果小师弟的博客 3774

hdu1841

/* 分析:     考虑问题一定要细心,不能想当然的。     一开始漏掉了一种情况,如果短的串可以嵌入长的串里面, 那么就不用考虑str1+str2、str2+str1这两种情况了。                                            2012-09-16 */ #include"stdio.h" #inc

Ice_Crazy的专栏 681

【OpenCV】背景替换(opencv、python),照片、证件照的背景替换(抠图)

目录步骤:导入库1、读入将要抠图的图片2、create一个带颜色域的mask3、通过mask使得原图只显示汽车,即抠出所需要的部分4、将要替换掉的图像resize或裁剪到和上述图片一样的大小5、将该图片预留出将要重叠上的部分,即将mask部分改为(0,0,0)6、通过和运算使得两部分图片mask部分替换7、最后保存图片 步骤: 导入库 import matplotlib.pyplot as plt import matplotlib.image as mpimg import numpy as np imp

weixin_41729258的博客 4364

对称加密中GCM和CBC俩种加密模式的区别

**CBC(Cipher Block Chaining)** 和 **GCM(Galois/Counter Mode)** 是两种常见的对称加密模式。它们的主要区别体现在加密方式、性能和安全性上。

闲人 1118

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

ARM功耗管理软件之DVFS&AVS

思考:功耗管理软件栈及示例?WFI&WFE?时钟&电源树?DVFS&AVS?

AI-安全-功耗 1236

hdu1841(KMP)

就是求包含两个给定串的最短串。那长度最长是len1+len2,如果有重叠的就减掉就最短了。拿s2匹配一次s1,再拿s1匹配一次s2,记录最大重叠。 #include #include #define N 1000010 int tst,fail[N]; char s1[N],s2[N]; inline int max(int x,int y){return x>y?x:y;} inline

Icefox的博客 474

OpenClaw赚钱实录:从“养龙虾“到可持续变现的实践指南——原生“龙虾”OpenClaw快速部署、问题速查与避坑指南

至此,你已经完成了从选购服务器到配置AI大脑的全过程。无论是想在云上养一只7x24小时的“龙虾”,还是在本地体验AI Agent的魅力,上述指南都提供了明确的路径。下一步,就是接入飞书、钉钉或QQ,让你的OpenClaw真正开始“干活”赚钱了 [citation:6]。

冯·诺依曼 995

2026最新YOLO26改进:卷积层、轻量化、注意力机制、损失函数、Backbone、SPPF、Neck、检测头全方位优化汇总

针对YOLO26模型改进痛点,本专栏整理200+实战验证方法,覆盖卷积层、注意力机制等核心模块。 每种方法含原理、性能分析、改进路径及实操流程,代码可直接运行,快速提升模型效果,性价比超高。

魔改工程师的博客 1万+

hdu 1841

Find the Shortest Common Superstring    Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)    Total Submission(s): 1120 Accepted Submission(s): 286 Problem Descrip

小磊's blog 1136

第77步 时间序列建模实战:多因素预测 vol-2(以决策树回归为例)

第77步 时间序列建模实战:多因素预测 vol-2(以决策树回归为例)

Jet4505的博客 1011

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

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

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

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

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

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

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

戳这里

WSMYE的博客 (hemiaomiao.me) 316

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

题目链接http://acm.hdu.edu.cn/showproblem.php?pid=1841 本题的意思就是给出两个字符串,看一下能组成最短的串的长度,当然只能首尾连接 这题也是对KMP的应用,主要还是对next数组的的求解 这题大概应该有两个地方需要注意的 1.开始写这题我直接就把串连接为S=S1+S2和S=S2+S1这两种情况,然后比较取S串最短的那种 比如样例中alba和b...

weixin_30952103的博客 77

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

hdu 1841 Find the Shortest Common Superstring

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

weixin_30378623的博客 87

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

HDU1841 Find the Shortest Common Superstring(两次KMP)

链接: http://acm.hdu.edu.cn/showproblem.php?pid=1841 题意: 两个字符串拼接,问拼成新串的最小长度,使得新串包含两个原串。 思路: 跑两遍kmp,找最长前缀。(先排除包含情况) #include &lt;bits/stdc++.h&gt; using namespace std; const int maxn = 1e6+10; int...

刘二狗的博客 395

HDU - 1841 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): 2126    Accepted Submission(s): 585 Problem Descr

DlPF_C的博客 303

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

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

天行健的博客 462
上一篇: 素数打表法 高效率
下一篇: hdu 2594 Simpsons’ Hidden Talents
korun
博客等级 码龄13年 13粉丝 24原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址