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. Windows 2016 安装单机版本Oracle ASM 的简单说明

    发现这样弄完 启动之后 就挂了 真蛋疼.  改天再研究一下. 1. 需要给磁盘处理一下 建议使用压缩卷的模式进行处理 如图示 需要新建简单卷 注意设置 然后不进行格式化 2. 然后安装oracle的g ...

  2. table表格的无缝循环

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. mysql行(记录)的详细的操作

    目录 一 介绍 二 插入(增加)数据INSERT 三 更新(修改)数据UPDATE 四 删除数据DELETE 五 查询数据SELECT(重点) 六 权限管理 阅读目录 一 介绍 MySQL数据操作: ...

  4. scrapy爬取猫眼电影排行榜

    做爬虫的人,一定离不开的一个框架就是scrapy框架,写小项目的时候可以用requests模块就能得到结果,但是当爬取的数据量大的时候,就一定要用到框架. 下面先练练手,用scrapy写一个爬取猫眼电 ...

  5. java读取blob全身乱码

    一.BLOB操作 .入库 ()JDBC方式 //通过JDBC获得数据库连接 Class.forName("oracle.jdbc.driver.OracleDriver"); Co ...

  6. 基于Websocket的websocketd

    WebSocket是什么 WebSocket是HTML5下面的一种技术,设计出来的目的就是要取代轮询和 Comet 技术,使客户端浏览器具备像 C/S 架构下桌面系统的实时通讯能力. 浏览器通过 Ja ...

  7. Slimvoice快速而小巧

    这可行吗?绝对没问题.完全加载的最大页面只有230 KB.因为所有内容都被缓存和压缩,所以随后查看的每个页面只有大约6 KB,这比我见过的具有相同功能的SPA要小得多. Slimvoice快速而小巧, ...

  8. ES6基本常见语法

    特色:写法更加优雅,更加像面像对象的编程,其思想和 ES5 是一致的. 箭头函数.this ES6中可以使用 => 作为函数表达形式,极简风格,参数+ => +函数体. var foo = ...

  9. Ubuntu18.10中pip install mysqlclient 出现EnvironmentError: mysql_config not found错误

    Complete output from command python setup.py egg_info: sh: 1: mysql_config: not found Traceback (mos ...

  10. aiomysql实现对数据库异步读取

    有一个库叫做aiomysql,这是一个基于asyncio和pymysql的库.至于为什么可以在tornado中使用,是因为高版本tornado的底层使用了asyncio. import asyncio ...