E. Test

time limit per test2 seconds

memory limit per test256 megabytes

inputstandard input

outputstandard output

Sometimes it is hard to prepare tests for programming problems. Now Bob is preparing tests to new problem about strings — input data to his problem is one string. Bob has 3 wrong solutions to this problem. The first gives the wrong answer if the input data
contains the substring s1, the second enters an infinite loop if the input data contains the substring s2, and the third requires too much memory if the input data contains the substring s3. Bob wants these solutions to fail single test. What is the minimal
length of test, which couldn't be passed by all three Bob's solutions?





Input

There are exactly 3 lines in the input data. The i-th line contains string si. All the strings are non-empty, consists of lowercase Latin letters, the length of each string doesn't exceed 105.





Output

Output one number — what is minimal length of the string, containing s1, s2 and s3 as substrings.





Examples

input

ab

bc

cd

output

4

input

abacaba

abaaba

x

output

11


这道题目是典型的kmp应用。我们知道kmp是快速判断模式字符串是否在目标字符串中存在。先回顾一下kmp算法:
kmp算法最核心的就是next数组,写这道题目之前又把kmp算法给看了一遍:next数组实际上模式字符串中当前位置前面的字符串前缀和后缀公共的最大长度。这个比较抽象和理论,其实next数组还可以理解成模式字符串的对称程度,对称程度越高,next数组越大。在求解next数组的时候,若前面一个next数,为0,那么说明前面没有对称的,新加的字符如果要对称只可能和第一个字符开始比较。如果next数不为0,说明前面一个字符是有和它对称的,那么去找和他对称的字符的下一个字符,如果相等那么next值就++,如果不相等只能等于0了。
下面是代码实现:
void getNext(string P)
{
int len=P.length();
Next[0]=0;
for(int i=1;i<len;i++)
{
int k=Next[i-1];
while(P[i]!=P[k]&&k!=0)
k=Next[k-1];
if(P[i]==P[k])
Next[i]=k+1;
else
Next[i]=0;
}<pre name="code" class="html">

}


还有一个更加精简的kmp算法

void getnext(string P )
{
int j = -1, i = 0;
int len=P.length();
next[0] = -1;
while(i < len)
{
if(j == -1 ||P[i] == P[j])
{
i++;
j++;
next[i] = j;
}
else
j = next[j];
} }

这个两个求next数组算法虽然求得的next数组不太一样,但是都可以用于kmp。下面给kmp的算法

int kmp(string T,string P)
{
int pLen=P.length();
int tLen=T.length();
int i=0,j=0;
while(i<pLen&&j<tLen)
{
if(i==-1||P[i]==T[j])
i++,j++;
else
i=next[i];
}
if(i>=pLen) return j-pLen+1;
        else  return -1;
</pre>接下来看这道题目:题目实际上是要求两字符串头尾想拼接,也即是头尾重合的最大长度。只需要在kmp的基础上稍加改动即可,下面给出AC代码</div><div class="problem-statement" style="margin:0.5em; padding:0px; line-height:1.5em; font-size:1.4rem"></div><div class="problem-statement" style="margin:0.5em; padding:0px; line-height:1.5em; font-size:1.4rem"><pre name="code" class="html">#include <iostream>
#include <string.h>
#include <string>
#include <algorithm>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
using namespace std;
#define MAX 100000
int _next[3][MAX+5];
string a[3];
int b[3][3];
int c[3];
void get_next(string P,int pos )
{
int j = -1, i = 0;
int len=P.length();
_next[pos][0] = -1;
while(i < len)
{
if(j == -1 ||P[i] == P[j])
{
i++;
j++;
_next[pos][i] = j;
}
else
j = _next[pos][j];
} }
int kmp(string T,string P,int pos)
{
int pLen=P.length();
int tLen=T.length();
int i=0,j=0;
while(i<pLen&&j<tLen)
{
if(i==-1||P[i]==T[j])
i++,j++;
else
i=_next[pos][i];
}
//cout<<i<<endl;
return i;
} int main()
{
cin>>a[0]>>a[1]>>a[2];
c[0]=a[0].length();c[1]=a[1].length();c[2]=a[2].length();
memset(_next,0,sizeof(_next));
get_next(a[0],0);get_next(a[1],1);get_next(a[2],2);
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
if(i!=j)
b[i][j]=kmp(a[i],a[j],j);
int ans=0;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
if(i==j)
continue;
for(int k=0;k<3;k++)
{
if(k==i||k==j)
continue;
if(b[i][j]==c[j])
ans=max(ans,b[i][j]+b[i][k]);
else
ans=max(ans,b[i][j]+b[j][k]);
}
}
}
printf("%d\n",c[0]+c[1]+c[2]-ans);
return 0; }





CodeForeces 25E (kmp)的更多相关文章

  1. CodeForces 25E Test KMP

    Description Sometimes it is hard to prepare tests for programming problems. Now Bob is preparing tes ...

  2. codeforeces近日题目小结

    题目源自codeforeces的三场contest contest/1043+1055+1076 目前都是solved 6/7,都差了最后一题 简单题: contest/1043/E: 先不考虑m个限 ...

  3. KMP算法求解

    // KMP.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> using namespac ...

  4. 简单有效的kmp算法

    以前看过kmp算法,当时接触后总感觉好深奥啊,抱着数据结构的数啃了一中午,最终才大致看懂,后来提起kmp也只剩下“奥,它是做模式匹配的”这点干货.最近有空,翻出来算法导论看看,原来就是这么简单(先不说 ...

  5. KMP算法

    KMP算法是字符串模式匹配当中最经典的算法,原来大二学数据结构的有讲,但是当时只是记住了原理,但不知道代码实现,今天终于是完成了KMP的代码实现.原理KMP的原理其实很简单,给定一个字符串和一个模式串 ...

  6. 萌新笔记——用KMP算法与Trie字典树实现屏蔽敏感词(UTF-8编码)

    前几天写好了字典,又刚好重温了KMP算法,恰逢遇到朋友吐槽最近被和谐的词越来越多了,于是突发奇想,想要自己实现一下敏感词屏蔽. 基本敏感词的屏蔽说起来很简单,只要把字符串中的敏感词替换成"* ...

  7. [KMP]【学习笔记】

    Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 36916   Accepted: 14904 Descript ...

  8. KMP算法实现

    链接:http://blog.csdn.net/joylnwang/article/details/6778316 KMP算法是一种很经典的字符串匹配算法,链接中的讲解已经是很明确得了,自己按照其讲解 ...

  9. KMP专题

    1.[HDU 3336]Count the string(KMP+dp) 题意:求给定字符串含前缀的数量,如输入字符串abab,前缀是a.ab.aba.abab,在原字符串中出现的次数分别是2.2.1 ...

随机推荐

  1. 【转】WCF入门教程三[WCF的宿主]

    一.WCF服务应用程序与WCF服务库 我们在平时开发的过程中常用的项目类型有“WCF 服务应用程序”和“WCF服务库”. WCF服务应用程序,是一个可以执行的程序,它有独立的进程,WCF服务类契约的定 ...

  2. http的GET和POST

    本文主要内容 1.  GET和POST方法介绍 2.  源代码分析 3.  结果分析 4.  例子参考及引用: http://www.cnblogs.com/zhijianliutang/archiv ...

  3. linux -- 进程的查看、进程id的获取、进程的杀死

    进程查看 ps ax : 显示当前系统进程的列表 ps aux : 显示当前系统进程详细列表以及进程用户 ps ax|less : 如果输出过长,可能添加管道命令 less查看具体进程, 如:ps a ...

  4. CentOS安装emacs24.2命令

    CentOS安装emacs24.2命令 #1.安装如下软件 yum -y groupinstall "Development Tools" yum -y install gtk+- ...

  5. Unity对象查找

    1. GameObject.Find  全局摄像机 全局画布 全局灯光 无法查找隐藏对象 ,效率低下,要用完全的路径来提升查找效率 2. transform.Find  UI中全部使用此方法 可以查找 ...

  6. C#获取IP信息

    /// <summary> /// 通过IP得到IP所在地省市(Porschev) /// </summary> /// <param name="ip&quo ...

  7. Faster-RCNN

  8. JavaScript匿名函数和回调函数

    匿名函数的自调函数格式: (function(){ //代码 })(); <script type="text/javascript"> (function(){ al ...

  9. CSS使用经验总结

    清除图片下方出现几像素的空白间隙 方法1: img{display:block;} 方法2: img{vertical-align:top;} 除了top值,还可以设置为text-top | midd ...

  10. ubis文件系统的制作

    在linux-2.6.27以前,谈到Flash文件系统,大家很多时候多会想到cramfs.jffs2.yaffs2等文件系统.它们也都是基于文件系统+mtd+flash设备的架构.linux-2.6. ...