hdu-1941 Find the Shortest Common Superstring
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的更多相关文章
- HDU 1841 Find the Shortest Common Superstring----KMP
题意:给两个字符串,问包含这两个字符串的最小的字符串的长度 kmp返回匹配串长度 #include "iostream" #include<cstdio> #inclu ...
- [LeetCode] 1092. Shortest Common Supersequence
LeetCode刷题记录 传送门 Description Given two strings str1 and str2, return the shortest string that has bo ...
- HDU 1941 Hide and Seek(离散化+树状数组)
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1941 题意:给出平面上n个点,找出一点p,使得距离p最近和最远的点的距离之差最小.输出这 ...
- HDU - 4725 (The Shortest Path in Nya Graph)层次网络
题意:有n个点,每个点都在一个层内,层与层之间的距离为c,一个层内的点可以到达与它相邻的前后两个层的点,还有m条小路 ..时间真的是卡的很恶心啊... 借一下别人的思路思路: 这题主要难在建图上,要将 ...
- Java基础常见英语词汇
Java基础常见英语词汇(共70个) ['ɔbdʒekt] ['ɔ:rientid]导向的 ['prəʊɡræmɪŋ]编程 OO: object ...
- 算法设计手冊(第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 ...
- 看到了必须要Mark啊,最全的编程中英文词汇对照汇总(里面有好几个版本的,每个版本从a到d的顺序排列)
java: 第一章: JDK(Java Development Kit) java开发工具包 JVM(Java Virtual Machine) java虚拟机 Javac 编译命令 java ...
- 专业英语词汇(Java)
abstract (关键字) 抽象 ['.bstr.kt] access vt.访问,存取 ['.kses]‘(n.入口, ...
- JAVA常用单词
柠檬学院Java 基础常见英语词汇(共 70 个)OO: object-oriented ,面向对象 OOP: object-oriented programming,面向对象编程JDK:Java d ...
随机推荐
- Golang应用性能问题排查分析
背景 公司有一个使用golang开发的采集模块,负责调用多个外部系统采集数据:最近做了一次架构上的调整,将采集模块分成api.job两个子模块,并部署到容器中,拆分前部署在虚机上. 现象 部分采集任务 ...
- Chrome Performance性能分析面板使用
最近做的项目都是内嵌egret游戏,想在移动端监测下它的性能,于是就开始了对Performance的探索: 一.使用 打开控制台,一顿操作: 网络选择Fast 3G,模拟手机普通3G环境,虽然现在大家 ...
- Nginx基础环境搭建
1.下载docker toolbox https://mirrors.aliyun.com/docker-toolbox/windows/docker-toolbox/ 2.选择好安装目录 一路nex ...
- JS实现植物大战僵尸小游戏,代码记录及效果展示
前几天看到了一个很有趣的demo,用js制作植物大战僵尸小游戏,本着学习的心态,对照着做了一下,发现这里面的一些代码设计的确很精妙,这里分享下源码和效果,如果有需要,可以看下. 效果如下: 下载地址
- centos7服务器远程安装图形化页面
以下是我用云的centos 7.4的安装步骤,照着我的命令操作肯定OK的,其他的就不敢保证了... 1.首先安装X(X Window System),命令为:(注意有引号) yum groupinst ...
- 浅析Linux用户空间中的Mmap
一.MMap基础概念 mmap是一种内存映射文件的方法,即将一个文件或者其它对象映射到进程的地址空间,实现文件磁盘地址和进程虚拟地址空间中一段虚拟地址的一一对映关系.实现这样的映射关系后,进程就可以采 ...
- 4、剑指offer——从尾到头打印链表java实现
**题目描述** **输入一个链表,按链表从尾到头的顺序返回一个ArrayList.** 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M 思路: 1.如果链 ...
- 【LinuxShell】命令行常用快捷键
Ctrl + A :光标跳到一行命令的开头.一般来说,Home 键有相同的效果: Ctrl + E :光标跳到一行命令的结尾.一般来说,End 键有相同的效果:. Ctrl + U :删除所有在光标左 ...
- Spring听课笔记(专题二下)
第4章 Spring Bean基于注解的装配 4.1 Bean的定义及作用域的注解实现 1. Bean定义的注解 -- @Component是一个通用注解,可用于任何bean -- @Reposito ...
- csv的读写操作
cvs简介: CSV 全称 Comma-Separated Values,中文叫逗号分隔值或字符分隔值,它以纯文本形式存储表格数据(数字和文本),其本质就是一个字符序列,可以由任意数目的记录组成,记录 ...