Time Limit: 2000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u

Submit Status

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

Input
abacaba
abaaba
x
Output

Source

题意:将3个字符串连接起来,重复的可以重叠不用计算,输出最短的长度
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 100000+20;
char s[4][maxn];
int next[4][maxn],len[5]; void get_next(int i)
{
int k=-1,j=0;
next[i][0]=-1;
while(j<len[i])
if(k==-1||s[i][j]==s[i][k])
{
k++;
j++;
next[i][j]=k;
}
else k=next[i][k];
} int kmp(int a,int b)
{
int i=0,j=0;
while(j<len[b]&&i<len[a])
if(i==-1||s[a][i]==s[b][j])
{
i++;j++;
}
else i=next[a][i];
return i;
} int main()
{
while(~scanf("%s %s %s",s[0],s[1],s[2]))
{
int ans=0;
len[0]=strlen(s[0]);
len[1]=strlen(s[1]);
len[2]=strlen(s[2]);
for(int i=0;i<3;i++) get_next(i); 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;
int x=kmp(i,j);
int y1=kmp(k,i);
int y2=kmp(k,j);
ans=max(ans,max(x+y1,x+y2));
}
}
printf("%d\n",len[0]+len[1]+len[2]-ans);
}
return 0;
}

  分析:只要求出三个字符串所能得到的最大匹配数就好,

CF 25 E 三个字符串 KMP模板的更多相关文章

  1. 模板—字符串—KMP(单模式串,单文本串)

    模板—字符串—KMP(单模式串,单文本串) Code: #include <cstdio> #include <cstring> #include <algorithm& ...

  2. 字符串系列——KMP模板整理

    KMP模板整理 KMP与扩展KMP: /*vs 2017/ vs code以外编译器,去掉windows.h头文件和system("pause");*/ #include<i ...

  3. <Django> MVT三大块之Template(模板)

    1.模板简介 创建项目,基本配置 第一步:配置数据库 第二步:创建APP,配置APP 第三步:配置模板路径 第四步:配置分发urls.py(APP里面的) 根目录下,增加命名空间namespace,作 ...

  4. ytu 1064: 输入三个字符串,按由小到大的顺序输出(水题,字符串处理)

    1064: 输入三个字符串,按由小到大的顺序输出 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 471  Solved: 188[Submit][Sta ...

  5. iOS开发Swift篇—(三)字符串和数据类型

    iOS开发Swift篇—(三)字符串和数据类型 一.字符串 字符串是String类型的数据,用双引号""包住文字内容  let website = "http://www ...

  6. kmp模板 && 扩展kmp模板

    kmp模板: #include <bits/stdc++.h> #define PB push_back #define MP make_pair using namespace std; ...

  7. 洛谷-p5410-扩展KMP模板

    链接: https://www.luogu.org/problem/P5410#submit 题意: 有两个字符串aa,bb,要求输出bb与aa的每一个后缀的最长公共前缀 思路: 扩展kmp模板, 上 ...

  8. Python第三章-字符串

    第三章  字符串 3.1 基本字符串操作 Python的字符串和元组差不多,是不可以进行改变的,如果想改变值,可以尝试list序列化之后在进行修改. {    website = 'http://ww ...

  9. python--基础学习(三)字符串单引号、双引号、三引号

    1.基本认识 单引号字符串:'python' 双引号字符串:"python" 三引号字符串:'''python'''(三单引号),"""python& ...

随机推荐

  1. Shell初学(五)bash shell的基本功能

    记住,所谓的bash shell 并不单纯指的是shell脚本,其实是Linux系统的所有指令集. shell脚本 只是汇总了指令集到文件,然后按流程和顺序执行. [1]如何查看我们的预设shell ...

  2. mysql下的sqlmode详解

    转自:https://www.cnblogs.com/Zender/p/8270833.html 阅读目录 一,sql_mode值的含义 二,ANSI模式 三,STRICT_TRANS_TABLES模 ...

  3. ThreadPoolExecutor的重要参数

    一.ThreadPoolExecutor的重要参数 1.corePoolSize:核心线程数         * 核心线程会一直存活,及时没有任务需要执行         * 当线程数小于核心线程数时 ...

  4. HDU 2041 DP

    URL:https://vjudge.net/problem/HDU-2041 简单DP,因为每次只能走1或者2阶,所以当走到第i阶的时候(i>=4),那么它的前一种状态只可能是i-1和i-2, ...

  5. Android Monkey测试入门<1>

    第一步:搭建环境:主要是安装和搭建java和sdk环境,说白了,对我们安卓开发来说,只要搭建好了Android开发环境,Monkey测试环境基本就是OK的了.可以参考:http://www.cnblo ...

  6. 测试基础_<一>

    1: 过程决定质量, 测试过程贯穿整个软件开发声明周期; 2: 测试过程和开发过程在整个开发周期相辅相成; 3: 测试过程是对整个开发过程的验证, 二者互相依赖 4: 测试过程是整个测试活动中一个至关 ...

  7. 纯CSS实现tag彩色标签

    利用纯CSS实现彩色tag标签,效果如下图 代码如下: .items a:nth-child(9n){background-color: #4A4A4A;} .items a:nth-child(9n ...

  8. Hive 教程(八)-hiveserver2

    hive 的另外一种启动方式是 hiveserver2,它是提供了一种服务,使得我们可以远程操作 hive,就像操作 mysql 一样 hiveserver1 既然有 hiveserver2,肯定有 ...

  9. 使用Python基于TensorFlow的CIFAR-10分类训练

    TensorFlow Models GitHub:https://github.com/tensorflow/models Document:https://github.com/jikexueyua ...

  10. 如何在Python中快速画图——使用Jupyter notebook的魔法函数(magic function)matplotlib inline

    如何在Python中快速画图--使用Jupyter notebook的魔法函数(magic function)matplotlib inline 先展示一段相关的代码: #we test the ac ...