HDU 1841 Find the Shortest Common Superstring KMP

RK3588, FFmpeg 拉流 RTSP, mpp 硬解码转RGB 阅读详情

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1841


题目有问题题、目有问题——代码1和代码3为什么不能过


题意:给你两个字符串,求解一个最短的字符串,两个字符串连接的时候相同的可以重叠

例如: “alba” 和“bacau”重叠在一起的最短串是 “albacau”。


我感觉这道题用KMP,求next数组就行,先将两个字符串连接起来(两种方法),然后求解next[len],当这个值大于len1或len2时,

继续求解next,最后用len-两个中最大的值,但是这样WA了,个人感觉没错,求解释。

WA代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <list>
#include <deque>
#include <queue>
#include <iterator>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <cctype>
using namespace std;

typedef __int64 LL;
const int N=1000005;
const LL II=1000000007;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);

char word[2*N],s1[N],s2[N];
int len,next[2*N];

void getnext(char *p,int wlen)
{
    int j=0,k=-1;
    next[0]=-1;
    while(j<wlen)
    {
        if(k==-1||p[j]==p[k])
        {
            j++;    k++;
            next[j]=k;
        }
        else
            k=next[k];
    }
}

int main()
{
    int ca=0,i,j,T;
    scanf("%d",&T);
    while(T--)
    {
        int len1,len2,t1,t2;
        scanf("%s%s",s1,s2);
        len1=strlen(s1);
        len2=strlen(s2);
        len=len1+len2;

        strcpy(word,s1);
        strcat(word,s2);
        word[len]='\0';
        getnext(word,len);
        t1=next[len];
        while(t1>len1)
            t1=next[t1];

        strcpy(word,s2);
        strcat(word,s1);
        word[len]='\0';
        getnext(word,len);
        t2=next[len];
        while(t2>len2)
            t2=next[t2];

        t1=max(t1,t2);
        printf("%d\n",len-t1);
    }
    return 0;
}




看了别人的博客,发现在这个之前加上一个判断包含关系就过了,

我感觉我上面这个代码也可以判断包含关系啊。

求解释,吾当以身相许

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <list>
#include <deque>
#include <queue>
#include <iterator>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <cctype>
using namespace std;

typedef __int64 LL;
const int N=1000005;
const LL II=1000000007;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);

char word[2*N],s1[N],s2[N];
int len,next[2*N];

void getnext(char *p,int wlen)
{
    int j=0,k=-1;
    next[0]=-1;
    while(j<wlen)
    {
        if(k==-1||p[j]==p[k])
        {
            j++;    k++;
            next[j]=k;
        }
        else
            k=next[k];
    }
}

int kmp(char *text,char *word)
{
    int i=0,j=0,k=0,wlen=strlen(word),tlen=strlen(text);
    while(i<tlen)//i指向text,j指向word
    {
        if(j==-1||text[i]==word[j])
        {       i++;j++;        }
        else
            j=next[j];      // 当j=0时,next[j]=-1
        if(j==wlen)
            return tlen;
    }
    return 0;//检查text串中含有word串的个数
}

int main()
{
    int ca=0,i,j,T;
    scanf("%d",&T);
    while(T--)
    {
        int len1,len2,t1,t2;
        scanf("%s%s",s1,s2);
        len1=strlen(s1);
        len2=strlen(s2);
        len=len1+len2;

        if(len1>len2)
        {
            swap(len1,len2);
            strcpy(word,s1);
            strcpy(s1,s2);
            strcpy(s2,word);
        }
        getnext(s1,len1);
        int Min=kmp(s2,s1);
        if(Min>0)
        {
            printf("%d\n",Min);
            continue;
        }

        strcpy(word,s1);
        strcat(word,s2);
        word[len]='\0';
        getnext(word,len);
        t1=next[len];
        while(t1>len1||t1>len2)
            t1=next[t1];

        strcpy(word,s2);
        strcat(word,s1);
        word[len]='\0';
        getnext(word,len);
        t2=next[len];
        while(t2>len1||t2>len2)
            t2=next[t2];

        t1=max(t1,t2);
        printf("%d\n",len-t1);
    }
    return 0;
}


又看了HDU1867,一模一样的,然后果断又改了一遍,提交,果断又WA了,

真心不知道哪里错了,哎………………悲催………………

WA代码:(感觉是对的)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <list>
#include <deque>
#include <queue>
#include <iterator>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <cctype>
using namespace std;

typedef __int64 LL;
const int N=1000005;
const LL II=1000000007;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);

char s1[N],s2[N];
int len,next[N];

void getnext(char *p,int wlen)
{
    int j=0,k=-1;
    next[0]=-1;
    while(j<wlen)
    {
        if(k==-1||p[j]==p[k])
        {
            j++;    k++;
            next[j]=k;
        }
        else
            k=next[k];
    }
}

int kmp(char *text,char *word)
{
    int i=0,j=0,tlen=strlen(text);
    while(i<tlen)
    {
        if(j==-1||text[i]==word[j])
            j++,i++;
        else
            j=next[j];
    }
    return j;
}

int main()
{
    int ca=0,i,j,T;
    cin>>T;
    while(T--)
    {
        scanf("%s%s",s1,s2);
        int len1,len2,t1,t2;
        len1=strlen(s1);
        len2=strlen(s2);

        getnext(s2,len2);
        t1=kmp(s1,s2);//s2模式串
        getnext(s1,len1);
        t2=kmp(s2,s1);//s1模式串
        int len=len1+len2;
        printf("%d %d\n",t1,t2);
        if(t1==t2)
            printf("%d\n",len-t1);
        else
            printf("%d\n",t1>t2?len-t1:len-t2);
    }
    return 0;
}


visio安装包 Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。 立即下载

相关推荐

从原理图到PCB:手把手教你绘制DRV8301驱动板(附完整代码与避坑指南)

本文详细介绍了如何从原理图设计到PCB布局,手把手教你绘制DRV8301驱动板,特别适用于FOC(磁场定向控制)系统开发。内容涵盖元器件选型、电源树架构设计、栅极驱动电路、PCB布局技巧、焊接调试及代码配置,并附有完整代码与常见问题避坑指南,帮助工程师快速掌握DRV8301驱动板的设计与优化。

weixin_33716941的博客 397

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

电磁炉线圈加热仿真全流程:用Ansys Maxwell分析涡流效应与热损耗

本文详细介绍了使用Ansys Maxwell进行电磁炉线圈加热仿真的全流程。通过构建2D轴对称涡流场模型,深入分析了高频下的涡流效应、趋肤效应与热损耗分布,为优化线圈设计、提升加热效率与均匀性提供了量化依据和实用指南。

sss66的博客 976

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

【Gephi】初学者教程(一)「一步一步教你怎么画图」「值得放进收藏夹吃灰系列」

前言 由于最近博主日常接任务帮人画图,而最近的状况大家也知道…远程画着画着觉得双方在图的结构、调色等等诸多方面会有很多歧义,博主理解的往往与客户所言有些许偏差…当然这其中也是包括有客户并未接触过gephi而导致的一些原因吧emmmm 总而言之,今天突发奇想在这个时候出教程就是为了让大噶少走点弯路(虽然博主才是真的菜) 然后呢,想说的比较多,所以就多拆了几个章节咳咳… 朋友们如果急切需要什么的可以直...

修仙一时爽 早起头犯怅 4814

Matlab实战:用递归最小二乘法(RLS)搞定动态系统参数辨识(附完整代码)

本文通过Matlab实战,详细介绍了如何利用递归最小二乘法(RLS)进行动态系统参数辨识。文章从理论优势切入,提供了完整的仿真数据生成、RLS算法核心实现代码,并对参数收敛过程、预测误差及不同遗忘因子的影响进行了可视化分析与性能评估,为工程实践提供了清晰的指南。

weixin_29035405的博客 507

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

华为CCA架构软件栈VehicleStack解析:3层服务化设计与AOS/VOS双核OS

本文深入解析华为CCA架构下的VehicleStack软件平台,重点介绍其三层服务化设计(硬件抽象层、服务框架层、应用层)与AOS/VOS双核操作系统协同机制。该架构通过计算与通信的深度融合,推动汽车电子电气架构向集中式发展,为智能汽车提供灵活、高效的软件定义汽车解决方案。

weixin_33491377的博客 325

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

MATLAB Appdesigner开发独立桌面App全流程(一):以打开串口功能为例介绍Appdesigner的基本使用

本文详细介绍了Appdesigner的基本使用,以串口功能为例介绍按钮回调函数的实现以及控件之间通信的问题

Zhuwany的博客 3万+

《open3D+pyqt》点云读取、显示与保存

《open3D+pyqt》第一章:点云读取与显示一、效果展示二、环境配置三、代码3.1 qt designer设置3.2 生成ui,qrc的py文件3.3 py文件代码 一、效果展示 二、环境配置 环境配置:Anconda+pycharm+pyqt+open3d+pyqtgraph 显示界面:这里选择pyqtgraph 参考网上教程 三、代码 3.1 qt designer设置 界面布局参照《PCL+QT》教程,界面设置:布局如下: 布局参数参考: 3.2 生成ui,qrc的py文件 3.3 py

阁楼 7116

hdu1841

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

Ice_Crazy的专栏 681

Kappa函数:测量一致性并计算置信区间的R语言包

Kappa函数是一种常用的统计量,用于衡量两个评估者之间的一致性。在R语言中,有一个名为"vcd"的包提供了计算Kappa函数以及相应的置信区间的功能。本文将介绍如何使用"vcd"包来计算Kappa函数的值,并进一步计算置信区间。Kappa函数的值介于-1和1之间,值越接近1表示评估者之间的一致性越高,值越接近-1表示评估者之间的一致性越低,值接近0表示评估者之间的一致性接近于随机。通过以上步骤,我们成功地使用R语言中的"vcd"包计算了Kappa函数的值和置信区间。接下来,我们可以使用"vcd"包中的。

2301_79331230的博客 1879

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有详细解说

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

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

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

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

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

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

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

HDU-1841-Find the Shortest Common Superstring(kmp)

博主CSDN 题目链接 题意: 给出两个串,用这两个串组成一个新串,使新串包含这两个串,问这个新串的长度最小是多少; 题解: 显然,对于两个串A,B;A如果是B的子串或者B如果是A的子串的话,直接输出那个母串的长度即可,如果没有这种关系,那么看一个串的后缀是否是另一个串的前缀,如果某个串的后缀与另一个串的前缀的公共部分最长,则答案=A.length+B.length-公共长度,所以可以直接用km...

Anoyer的博客 377

文章标题 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

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

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

天行健的博客 462

HDU 1841 Find the Shortest Common Superstring----KMP

题意:给两个字符串,问包含这两个字符串的最小的字符串的长度 kmp返回匹配串长度 #include "iostream" #include<cstdio> #include<algorithm> #include<cstring> using namespace std; #define N 1000005 char s[N],t[N...

weixin_30412013的博客 113

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): 1693    Accepted Submission(s): 460 Problem Descr

几人憔悴几人泪 604

HDU 1841 Find the Shortest Common Superstring(KMP 理解 应用 求组合串的next值判断是否匹配)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1841 表示这个题目做起来还是有点意思的 首先分析一下解这个题目要考虑的两种情况 1、一个串在另外一个串中,那么结果就是两个串中长度长的哪一个 2、一个串的头部是另外一个串的尾部,这里分两种情况(每个串都有一头一尾嘛) 明白上面两点之后开始用KMP解题 首先直观的普通解法是两个串相互前后拼接

sunrain的专栏 809

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
上一篇: HDU 4474 Yet Another Multiple Problem 搜索——BFS
下一篇: HDU 1867 A + B for you again KMP
lamborghini1993
博客等级 码龄14年 166粉丝 289原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址