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];
int next1[N];
void get_next(char b[])
{
next1[]=-;
int i=;
int j=-;
int str=strlen(b);
while(i<str-)
{
if(j==-||b[i]==b[j])
next1[++i]=++j;
else
j=next1[j];
}
}
int kmp(char a[],char b[],int stra,int strb)
{
int i=;
int j=-;
get_next(b);///b为模式串
while(i<stra&&j<strb)
{
if(j==-||a[i]==b[j])
i++,j++;
else
j=next1[j];
}
return j;
}
int main()
{
int i,j,n,ans,cnt,ls,lt;
scanf("%d",&n);
while(n--)
{
scanf("%s%s",s,t);
ls=strlen(s);
lt=strlen(t);
ans=kmp(s,t,ls,lt);
cnt=kmp(t,s,lt,ls);
printf("%d\n",ls+lt-max(ans,cnt)); }
return ;
}
HDU 1841 Find the Shortest Common Superstring----KMP的更多相关文章
- hdu-1941 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 ch ...
- HDU 1711 Number Sequence (字符串匹配,KMP算法)
HDU 1711 Number Sequence (字符串匹配,KMP算法) Description Given two sequences of numbers : a1, a2, ...... , ...
- HDU 4644 BWT(Burrows–Wheeler transform+KMP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4644 题意:给出一个串,按照下面的步骤得到一个新串: (1)首先将其后面增加一个美元符号: (2)将每 ...
- HDU 1867 A + B for you again ----KMP
题意: 给你两个字符串,输出他们合并之后的字符串,合并的时候把A的后缀和B的前缀重叠合(或者把A的前缀和B的后缀重合).要求合并后的串既包含A右包含B, 且使得合并后的字符串尽量短,其次是使得合并后的 ...
- [LeetCode] 1092. Shortest Common Supersequence
LeetCode刷题记录 传送门 Description Given two strings str1 and str2, return the shortest string that has bo ...
- HDU - 4300 Clairewd’s message (拓展kmp)
HDU - 4300 题意:这个题目好难读懂,,先给你一个字母的转换表,然后给你一个字符串密文+明文,密文一定是全的,但明文不一定是全的,求最短的密文和解密后的明文: 题解:由于密文一定是全的,所以他 ...
- hdu 3336【Count the string】(KMP)
一道字符串匹配的题目,仅仅借此题练习一下KMP 因为这道题目就是要求用从头开始的n个字符串去匹配原来的字符串,很明显与KMP中求next的过程很相似,所以只要把能够从头开始匹配一定个数的字符串的个数加 ...
- hdu 5442 Favorite Donut 最大表示法+kmp
题目链接 给你一个字符串, 然后把他想象成一个环. 从某一个地方断开,然后逆时针或顺时针, 都可以形成一个字符串, 求字典序最大的那种. 输出断开位置以及是顺时针还是逆时针. 如果两个一样, 输出位置 ...
- hdu 4468 spy 极其精彩的一道kmp灵活运用题
出的超级好的一道题.至于好在哪里,请思考题目: 题意抽象出来为给定一个字符串r,找出它的一个最短后缀s,使得这个r可以被 s的某前缀+s的某前缀+......+s的某前缀+s本身构造出来. 具体题目描 ...
随机推荐
- 内置函数系列之 filter
filter 过滤 基本语法: s = filter(function,iterable) 将可迭代对象的每一个元素,传进函数中,根据函数中的判断条件,返回True或False 返回True的是保留的 ...
- php 图片操作类,支持生成缩略图,添加水印,上传缩略图
<?php class Image { //类开始 public $originimage = ""; //源图片文件地址 public $image ...
- python学习之循环语句
编程语言中的循环语句,以循环判断达式是否成立为条件,若表达式成立则循环执行该表达式,若不成立则跳出当前执行执行语句且继续执行其后代码. 如下图所示. Python中提供以下循环方式 循环类型 描述 w ...
- C语言数组篇(二)指针数组和数组指针
数组指针 和 指针数组 这两个名词可以说是经常搞混了 数组指针--> 数组的指针 就是前面讲的 指向数组a的指针p; 指针数组--&g ...
- C语言指针篇(一)指针与指针变量
指针 1. 什么是指针? 2. 指针可不可怕? 3. 指针好不好玩? 4. 怎么学好指针? C语言是跟内存打交道的语言,指针就是内存地址.指针无处不在,指针并不可怕,相反,等你学到一定程 ...
- Atlantis HDU - 1542
Problem Description There are several ancient Greek texts that contain descriptions of the fabled is ...
- Go实现try-catch-finally机制
前言 许多主流语言诸如:Java.Python都实现了try-catch-finally机制,而Go处理错误的方式却与前两种语言不同.关于Go处理异常的方式是好是坏仁者见仁智者见智,笔者还是更喜欢tr ...
- svn Previous operation has not finished; run 'cleanup' if it was interrupted
svn cleanup failed–previous operation has not finished; run cleanup if it was interrupted Usually, a ...
- CentOS 7.X 防火墙简单配置
CentOS7使用的是Linux Kernel 3.10.0的内核版本,新版的Kernel内核已经有了防火墙netfilter,并且使用效能更高,稳定性更好. 配置防火墙的两种方法: 一.使用xml配 ...
- python学习笔记十:异常
一.语法 #!/usr/bin/python filename='hello' #try except finally demo try: open('abc.txt') print hello ex ...