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 consecutive characters. For instance, the shortest common superstring of “alba” and “bacau” is “albacau”. 
Given two strings composed of lowercase English characters, find the length of their shortest common superstring. 

InputThe first line of input contains an integer number T, representing the number of test cases to follow. Each test case consists of 2 lines. The first of these lines contains the string S 1 and the second line contains the string S 2. Both of these strings contain at least 1 and at most 1.000.000 characters. 
OutputFor each of the T test cases, in the order given in the input, print one line containing the length of the shortest common superstring. 
Sample Input

2
alba
bacau
resita
mures

Sample Output

7
8
这道题让我对kmp又有了更深的理解,之前对a,b串之间的字符匹配的过程还有些模糊。
本体题意:求包含a,b两字符串的最小字符串的长度。
解题思路:a,b两字符串互相进行两次kmp,满足以下三个条件:
a的后缀与b的前缀重合
a的前缀与b的后缀重合
a在b内或b在a内

则记录此时nex数组的位置,并跳出循环,具体看代码:
#include <cstdio>
#include <cstring>
const int M = 1111111;
char st[M],str[M];
int nex[M],la,lb;
void getn(char *st){
int j=-1;
nex[0]=-1;
int le=strlen(st);
for(int i=1;i<le;i++){
while(j>-1&&st[j+1]!=st[i]) j=nex[j];
if(st[j+1]==st[i]) j++;
nex[i]=j;
}
}
int kmp(char *st,char *str){
getn(st);
int j=-1;
int le=strlen(st),len=strlen(str);
for(int i=0;i<len;i++){
while(j>-1&&st[j+1]!=str[i]) j=nex[j];
if(st[j+1]==str[i]) j++;
if(j==le-1) return j; //中间
}
return j; //前缀后缀
}
int t;
int main(){
scanf("%d",&t);
while(t--){
scanf("%s %s",st,str);
int sum=strlen(st)+strlen(str);
memset(nex,0,sizeof(nex));
int a=kmp(st,str)+1;
memset(nex,0,sizeof(nex));
int b=kmp(str,st)+1;
// printf("%d %d\n",a,b);
int maxx=(a>b)?a:b;
int ans=sum-maxx;
printf("%d\n",ans);
}
return 0;
}

记录下kmp的板子:戳这里

 

hdu-1941 Find the Shortest Common Superstring的更多相关文章

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

    题意:给两个字符串,问包含这两个字符串的最小的字符串的长度 kmp返回匹配串长度 #include "iostream" #include<cstdio> #inclu ...

  2. [LeetCode] 1092. Shortest Common Supersequence

    LeetCode刷题记录 传送门 Description Given two strings str1 and str2, return the shortest string that has bo ...

  3. HDU 1941 Hide and Seek(离散化+树状数组)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1941 题意:给出平面上n个点,找出一点p,使得距离p最近和最远的点的距离之差最小.输出这 ...

  4. HDU - 4725 (The Shortest Path in Nya Graph)层次网络

    题意:有n个点,每个点都在一个层内,层与层之间的距离为c,一个层内的点可以到达与它相邻的前后两个层的点,还有m条小路 ..时间真的是卡的很恶心啊... 借一下别人的思路思路: 这题主要难在建图上,要将 ...

  5. Java基础常见英语词汇

    Java基础常见英语词汇(共70个) ['ɔbdʒekt] ['ɔ:rientid]导向的                             ['prəʊɡræmɪŋ]编程 OO: object ...

  6. 算法设计手冊(第2版)读书笔记, Springer - The Algorithm Design Manual, 2ed Steven S.Skiena 2008

    The Algorithm Design Manual, 2ed 跳转至: 导航. 搜索 Springer - The Algorithm Design Manual, 2ed Steven S.Sk ...

  7. 看到了必须要Mark啊,最全的编程中英文词汇对照汇总(里面有好几个版本的,每个版本从a到d的顺序排列)

    java:  第一章: JDK(Java Development Kit) java开发工具包 JVM(Java Virtual Machine) java虚拟机 Javac  编译命令 java   ...

  8. 专业英语词汇(Java)

    abstract (关键字)             抽象 ['.bstr.kt] access                            vt.访问,存取 ['.kses]‘(n.入口, ...

  9. JAVA常用单词

    柠檬学院Java 基础常见英语词汇(共 70 个)OO: object-oriented ,面向对象 OOP: object-oriented programming,面向对象编程JDK:Java d ...

随机推荐

  1. SAP中的密码输入框

    在SAP中的密码输入框,可分为两种情况: 1.用selection语句书写的选择屏幕上的密码输入框 实现的方式就是在AT SELECTION-SCREEN OUTPUT事件中写入如下代码: LOOP ...

  2. MySQL数据库基础知识及优化

    MySQL数据库基础知识及优化必会的知识点,你掌握了多少? 推荐阅读: 这些必会的计算机网络知识点你都掌握了吗 关于数据库事务和锁的必会知识点,你掌握了多少? 关于数据库索引,必须掌握的知识点 目录 ...

  3. Linux Ubuntu系统版本通过Crontab设置定时任务的执行

    Linux Ubuntu系统版本通过Crontab设置定时任务的执行 本文由本人收集网络信息总结而来 特别鸣谢:https://linux.zone/2258 1 crontab 简单介绍以及语法使用 ...

  4. 超微服务器重置ipmi登录密码

    超微服务器的ipmi登录密码不对,需要重置但是bios内并没有找到可以设置的选项. 以下是解决办法: 安装IPMITOOLyum install ipmitool -y 执行以下命令加载模块:modp ...

  5. Shell从入门到精通

    熟悉基本shell操作不仅是运维的基本功,对于开发来说也是多多益善,我在学习的过程中,总结了十个练手的小demo,并附上涉及的知识点,仅供娱乐. 1. 多线程ping监控,检查同一网段的IP是否连通 ...

  6. jQuery 实现复制功能

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. 源码 redis 分布式锁

    https://github.com/SPSCommerce/redlock-py/tree/master/redlock

  8. CCDictionary 用调试器查看问题

    if(dic->objectForKey("uid")) uid = dic->valueForKey("uid")->getCString( ...

  9. 5.2 spring5源码--spring AOP源码分析二--切面的配置方式

    目标: 1. 什么是AOP, 什么是AspectJ 2. 什么是Spring AOP 3. Spring AOP注解版实现原理 4. Spring AOP切面原理解析 一. 认识AOP及其使用 详见博 ...

  10. java.awt.event.MouseEvent鼠标事件的定义和使用 以及 Java Swing-JTextArea的使用

    最近发现一个CSDN大佬写的Java-Swing全部组件的介绍:Java Swing 图形界面开发(目录) JTextArea 文本区域.JTextArea 用来编辑多行的文本.JTextArea 除 ...