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. 三十三:WEB漏洞-逻辑越权之水平垂直越权

    水平和垂直越权 水平越权:可以获得同级别用户权限 垂直权限:享受高几个层次的用户权限 解释,原理,检测,利用,防御 通过更换的某个ID之类的身份标识,从而使得A账号获取(修改,删除)B账号的数据,通过 ...

  2. 性能测试WAS内存使用的探索和分析

    性能测试中,CPU和内存是关注最多的两个性能指标.以我行应用最多的系统架构(WAS+Oracle)来说,CPU使用率高的问题多发生于数据库,比如索引不当引发的表扫描.绑定变量使用不当引发的硬解析.连接 ...

  3. 【UltraISO】中文破解版

    下载链接:https://cn.ultraiso.net/uiso9_cn.exe简体中文版专用:   注册名:Guanjiu    注册码:A06C-83A7-701D-6CFC多国语言版专用:   ...

  4. Python基础(列表、元组)

    列表 在Python中列表用[]来表示,中间的元素可以是任何类型,用逗号分隔.列表是可变类型. 列表常用操作:增删改查. names = ["小明","小红", ...

  5. 11.15 gryz校测(题解分析报告)

    T1 心有灵犀 (cooperate) 题目大意 给你一个不超过 \(10^9\) 的数字 \(n\) 和一个交换次数上限 \(k\), 每次操作对这个 数字 \(n\) 的其中两位进行交换, 比如 ...

  6. 基于Vue+ElementUI架构的前端国际化解决方案

    1.项目目录结构 ├── build                      构建相关配置文件 |     |── index.js             webpack的基础配置入口 ├── m ...

  7. Prometheus—告警altermanger

    Prometheus-告警altermanger 1.告警altermanger装配 2.告警Mysql 3.Prometheus针对nodes告警规则配置 相关内容原文地址链接: 51CTO:wfw ...

  8. linux shell 判断空字符串的几种方法!

    在书写linux shell 脚本我们经常会遇到,对一个字符串是否为空进行判断,下面我对几种常用的方法进行了一个总结: 1.-z判断 -z string True if the length of s ...

  9. java获取post请求头部字符串

    尝试过很多方式,下面的方式最有效: 用获取数据流的方式,直接获取post过来的所有数据流 // 读取请求内容 BufferedReader br = new BufferedReader(new In ...

  10. 封装各种生成唯一性ID算法的工具类

    /** * Copyright (c) 2005-2012 springside.org.cn * * Licensed under the Apache License, Version 2.0 ( ...