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. linux源码安装nodejs

    如何在linux上安装nodejs环境 到网下载安装文件node-v5.10.1-linux-x64.tar.gz.   将源码包上传到linux的/usr/local/node/目录下,在该目录下解 ...

  2. IOS 音频播放

    iOS音频播放 (一):概述 前言 从事音乐相关的app开发也已经有一段时日了,在这过程中app的播放器几经修改我也因此对于iOS下的音频播放实现有了一定的研究.写这个系列的博客目的一方面希望能够抛砖 ...

  3. Core Data数据持久性存储基础教程-备用

    摘要 就像我一直说的,Core Data是iOS编程,乃至Mac编程中使用持久性数据存储的最佳方式,本质上来说,Core Data使用的就是SQLite,但是通过一系列特性避免了使用SQL的一些列的麻 ...

  4. LeetCode_Subsets II

    Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...

  5. yii 的mvc工作流

    Yii 使用了 Web 开发中广泛采用的模型-视图-控制器(MVC)设计模式. MVC的目标是将业务逻辑从用户界面的考虑中分离,这样开发者就可以更容易地改变每一部分而不会影响其他. 在 MVC中,模型 ...

  6. hdu 4336 Card Collector(期望 dp 状态压缩)

    Problem Description In your childhood, people in the famous novel Water Margin, you will win an amaz ...

  7. pyqt 图片(label上显示

    # -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' from decimal import * from PyQt4.Q ...

  8. 平时的笔记02:处理fnmatch模块

    # Copyright 2006 Joe Wreschnig## This program is free software; you can redistribute it and/or modif ...

  9. (转) unity 在移动平台中,文件操作路径详解

    http://www.unitymanual.com/thread-23491-1-1.html 今天,这篇文章其实是个老生常谈的问题咯,在网上类似的文章也比比皆是,在此我只是做个详细总结方便大家能够 ...

  10. Qt之操作Excel

    Visual Basic for Applications(VBA)是一种Visual Basic的一种宏语言,主要能用来扩展Windows的应用程式功能,特别是Microsoft Office软件. ...