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 ...
随机推荐
- Doris
Doris 基本概念 Doris 是基于 MPP 架构的交互式 SQL 数据仓库,主要用于解决近实时的报表和多维分析. Doris 分成两部分 FE 和 BE ,FE 负责存储以及维护集群元数据.接收 ...
- uni-app通过canvas实现手写签名
分享一个uni-app实现手写签名的方法 具体代码如下: <template> <view > <view class="title">请在下面 ...
- 简易双色球dome分享
代码如下: <style type="text/css"> div {font-weight: bold;text-align: center;} .tone{widt ...
- java之 Request
0x01.Request 什么是request 在Servlet API中,定义了一个HttpServletRequest接口,它继承自ServletRequest接口,专门用来封装HTTP请求消息. ...
- 处理 K8S Orphaned pod found - but volume paths are still present on disk 孤儿pod
问题概述 查看kubelet或/var/log/messages日志一直包错,发现是孤儿pod,是由于其pod被删除后存储路径还保存在磁盘. 报错如下 [root@node5 ~]# journalc ...
- Vue 标签Style 动态三元判断绑定
<div :style=" 1==1 ? 'display:block' : 'display:none' "></div> v-bind:style 的 ...
- 虚拟化kvm的搭建
虚拟化, 是指通过虚拟化技术将一台计算机虚拟为多台逻辑计算机 ,在一台计算机上同时运行多个逻辑计算机,每台逻辑计算机可运行不同的操作系统,并且应用程序都可以在相互独立的空间内运行而互不 ...
- MySQL主从复制配置部署
配置前准备:安装MySQL MySQL在centOS上的安装传送门: 1.集群规划 hadoop105 hadoop106 hadoop107 MySQL(master) MySQL(slave) ...
- 网络编程中 TCP 半开连接和TIME_WAIT 学习
https://blog.csdn.net/chrisnotfound/article/details/80112736 上面的链接就是说明来 SO_KEEPALIVE 选项 为什么还需要 在应用层开 ...
- innodb和myisam原理
MyISAM索引实现 MyISAM引擎使用B+Tree作为索引结构,叶节点的data域存放的是数据记录的地址.如图: 这里设表一共有三列,假设我们以Col1为主键,则上图是一个MyISAM表的主索引 ...