Description

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 s1s2 and s3 as substrings.

Sample Input

Input
ab
bc
cd
Output
4
Input
abacaba
abaaba
x
Output
11

利用kmp匹配算法,在kmp匹配算法稍加修改就ok,因为这里可以只匹配一部分前缀,不用完全匹配。
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std; char str[1100000];
char s0[1100000],s1[1100000],s2[1100000];
int next[1100000];
void getnextval(char *p)//求一个字符串的next数组,结果保存在全局变量next[]中。
{
int plen=strlen(p);
next[0]=-1;
int k=-1;
int j=0;
while (j<plen-1)
{
if (k==-1||p[k]==p[j])
{
j++;
k++;
if (p[j]!=p[k])
next[j]=k;
else
next[j]=next[k];
}
else
{
k=next[k];
}
}
} int kmp(char *s,char *p)//以s为匹配串,p为模式串进行匹配,返回匹配后p剩下的字符个数。
{
int l;
int i=0;
int j=0;
int slen=strlen(s);
int plen=strlen(p);
getnextval(p);
while (i<slen&&j<plen)
{
if (j==-1||s[i]==p[j])
{
i++;
j++;
}
else
j=next[j];
}
if (j==plen)//完全匹配的情况,一个字符也不剩,放回0。
l=0;
else if (i==slen) //匹配一部分,也就是p的一个前缀和s的一个后缀完全匹配。
l=plen-j; else
l=plen-(slen-i)-1; return l;
} int cat(char *a,char *b,char *c) //假设a串在最前,b串在中间,c串在最后至少需要多长。
{
str[0]='\0';
int la=strlen(a);
int lb=strlen(b);
int lc=strlen(c);
int len=kmp(a,b);
strcat(str,a);
strcat(str,b+lb-len);
len=kmp(str,c);
return strlen(str)+len;
} int main()
{
while (scanf("%s%s%s",s0,s1,s2)!=EOF)
{
int minn;
int l;
//一下分6种情况讨论。
minn=cat(s0,s1,s2); l=cat(s0,s2,s1);
if (l<minn)
minn=l; l=cat(s1,s2,s0);
if (l<minn)
minn=l; l=cat(s1,s0,s2);
if (l<minn)
minn=l; l=cat(s2,s0,s1);
if (l<minn)
minn=l; l=cat(s2,s1,s0);
if (l<minn)
minn=l; cout <<minn<<endl;
}
return 0;
}

  对于kmp的学习推荐博客:http://blog.csdn.net/v_july_v/article/details/7041827,讲的很好。

CodeForces 25E Test KMP的更多相关文章

  1. Codeforces 25E Test 【Hash】

    Codeforces 25E Test E. Test Sometimes it is hard to prepare tests for programming problems. Now Bob ...

  2. codeforces 631D. Messenger kmp

    题目链接 首先想到kmp, 和普通的不一样的是,中间部分严格相等, 头和尾的字符相等但是数量可以不相等. 所以应该把子串的头和尾先去掉,然后对剩下的部分进行kmp. 子串长度为1或2要特别讨论. 不要 ...

  3. CODEFORCES 25E Test

    题意 三个字符串,找一个字符串(它的子串含有以上三个字符串)使它的长度最短,输出此字符串的长度. 题解 先枚举字符串排列,直接KMP两两匹配,拼接即可...答案取最小值.. 常数巨大的丑陋代码 # i ...

  4. Codeforces 126B. Password (KMP)

    <题目链接> 题目大意:给定一个字符串,从中找出一个前.中.后缀最长公共子串("中"代表着既不是前缀,也不是后缀的部分). 解题分析:本题依然是利用了KMP中next数 ...

  5. Codeforces 126B(kmp)

    要点 头尾的最长相同只要一个kmp即可得,于是处理中间部分 扫一遍记录一下前缀的每个位置是否存在一个中间串跟它相同,见代码 如果当前没有,接着用Next数组去一找即可 #include <cst ...

  6. Codeforces 1163D(kmp、dp)

    要点 \(dp[i][j][k]\)表示主串已经到第\(i\)位时,\(s\)匹配在\(j\)位.\(t\)匹配在\(k\)位的最大得分 本来就要试填一层循环,如果转移也写在循环里的化复杂度承受不了, ...

  7. Codeforces 625B【KMP】

     题意就是一个串在另一个串出现几次,但是字符不能重复匹配, 比如aaaaaaa aaaa的答案是1 思路: 本来写了个暴力过的,然后觉得KMP改改就好了,就让队友打了一个: #include < ...

  8. Codeforces 1163D DP + KMP

    题意:给你一个字符串s,以及两个字符串s1,s2.s中有些位置是*,意思是可以随便填字母,s的子串中如果出现一次s1,就加一分,如果出现一次s2,就减一分.问这个字符串s最多可以得多少分? 思路: 设 ...

  9. [codeforces] 25E Test || hash

    原题 给你三个字符串,找一个字符串(它的子串含有以上三个字符串),输出此字符串的长度. 先暴力判断是否有包含,消减需要匹配的串的数量.因为只有三个字符串,所以暴力枚举三个串的位置关系,对三个串跑好哈希 ...

随机推荐

  1. easy ui 实现gridview效果

    前台: // 加载审批步骤列表 function FillStep(flowID) { $('#tbStepList').datagrid({ url: "/System/ApproverS ...

  2. qt之fillder抓包(QT网络版有一些具体的坑)

    最近项目中使用到了Qt的网络库,在用的过程中也发现了不少坑和问题,本文仅仅作为记录,方便日后查阅.    因为我们整个客户端的gui都是使用qt来完成的,心想qt既然有网络库,而且真心觉着qt封装的控 ...

  3. Android 获取当前日期算前一年、前一月、前一天Calendar

    使用Calendar的add(int field, int amount)方法 Calendar ca = Calendar.getInstance();//得到一个Calendar的实例 ca.se ...

  4. MySQL加锁分析

    参考:MySQL 加锁处理分析.该文已经讲的很详尽了,也易懂,下面仅仅是个人做的总结. 一. 背景 1.1 隔离级别 1.2 加锁过程 逐条处理,逐条加锁. 1.3 两阶段锁2PL 1.4 gap锁 ...

  5. IOS开发错误提示原因集合-----长期更新

    "[__NSCFConstantString size]: unrecognized selector sent to instance." =>将NSString类型的参数 ...

  6. Java宝典(四)------Java中也存在内存泄露。

    --Java中会存在内存泄露吗? --如果你想当然的以为Java里有了垃圾回收机制就不会存在内存泄露,那你就错了. Java里也会存在内存泄露! 我们慢慢来分析. 所谓内存泄露就是指一个不再被程序使用 ...

  7. 简单dp hdu-4105-Electric wave

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4105 题目意思: 给一个字符串,求最大的分隔空格数,记所有被分隔的数为a1,a2,a3,..... ...

  8. PHP商城购物车类

    <?php /* 购物车类 */ // session_start(); class Cart { //定义一个数组来保存购物车商品 private $iteams; private stati ...

  9. 最长公共子串LCS(Longest Common Substring)

    一.问题描述 寻求两个字符串中的最大公共字串,其中子串是指字符串中连续的字符组成的,而不是像子序列,按照字符的前后顺序组成.如str1="sgabacbadfgbacst",str ...

  10. [core java学习笔记][第六章接口与内部类]

    接口域内部类 接口 描述类具有什么功能,不给出具体实现. 内部类 用于设计协作关系的类集合 代理 实现任意接口的对象. 6.1 接口 接口声明 public interface Comparable ...