CF 25 E 三个字符串 KMP模板
| Time Limit: 2000MS | Memory Limit: 262144KB | 64bit IO Format: %I64d & %I64u |
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 s1, s2 and s3 as substrings.
Sample Input
ab
bc
cd
abacaba
abaaba
x
Source
#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模板的更多相关文章
- 模板—字符串—KMP(单模式串,单文本串)
模板—字符串—KMP(单模式串,单文本串) Code: #include <cstdio> #include <cstring> #include <algorithm& ...
- 字符串系列——KMP模板整理
KMP模板整理 KMP与扩展KMP: /*vs 2017/ vs code以外编译器,去掉windows.h头文件和system("pause");*/ #include<i ...
- <Django> MVT三大块之Template(模板)
1.模板简介 创建项目,基本配置 第一步:配置数据库 第二步:创建APP,配置APP 第三步:配置模板路径 第四步:配置分发urls.py(APP里面的) 根目录下,增加命名空间namespace,作 ...
- ytu 1064: 输入三个字符串,按由小到大的顺序输出(水题,字符串处理)
1064: 输入三个字符串,按由小到大的顺序输出 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 471 Solved: 188[Submit][Sta ...
- iOS开发Swift篇—(三)字符串和数据类型
iOS开发Swift篇—(三)字符串和数据类型 一.字符串 字符串是String类型的数据,用双引号""包住文字内容 let website = "http://www ...
- kmp模板 && 扩展kmp模板
kmp模板: #include <bits/stdc++.h> #define PB push_back #define MP make_pair using namespace std; ...
- 洛谷-p5410-扩展KMP模板
链接: https://www.luogu.org/problem/P5410#submit 题意: 有两个字符串aa,bb,要求输出bb与aa的每一个后缀的最长公共前缀 思路: 扩展kmp模板, 上 ...
- Python第三章-字符串
第三章 字符串 3.1 基本字符串操作 Python的字符串和元组差不多,是不可以进行改变的,如果想改变值,可以尝试list序列化之后在进行修改. { website = 'http://ww ...
- python--基础学习(三)字符串单引号、双引号、三引号
1.基本认识 单引号字符串:'python' 双引号字符串:"python" 三引号字符串:'''python'''(三单引号),"""python& ...
随机推荐
- windows10 AppStore安装 应用商店重新安装
点击左下角的搜索按钮,如下图所示 输入powershell,在结果中找到widows powershell应用,如下图所示 右键单击widows powershell应用,选择以管理员运行,如 ...
- springmvc默认配置文件
当在新建的maven web项目的web.xml中直接加入下面的<servlet>和<servlet-mapping>后,直接运行就会出现这个报错,意思就是找不到默认的spri ...
- BugkuCTF--never give up
这道题还挺有意思的... http://123.206.87.240:8006/test/hello.php 查看元素,有个1p.html,访问. 还没看到网页元素就跳转了...抓包! 抓到了一堆东西 ...
- 版本控制器之SVN(二)
安装重启以后,在菜单栏找到TortoiseSVN程序 启动以后 点击: 填写相应的信息: 可以看到项目的相关信息 选中仓库,右键 > Browse Repository 进入如下界面: 可以打开 ...
- 学习笔记--APIO 2018 二分专题 By wuvin
前言: 在APIO 2018 Day2下午听wuvin讲二分,听了一上午的神仙,现在终于有可以听懂了. 专题: 平均边权最大 题目链接:https://www.questoj.cn/problem/3 ...
- opencv中的高维矩阵Mat
本示例程序主要是通过实例演示高维Mat的寻址方式. //3,4分别表示行数.列数,所以3*4是一个页面的元素数,2表示有2个3*4 ,b=,c=; int size[]={a,b,c}; float* ...
- CSS行高——line-height 垂直居中等问题
CSS行高——line-height 初入前端的时候觉得CSS知道display.position.float就可以在布局上游刃有余了,随着以后工作问题层出不穷,才逐渐了解到CSS并不是几个sty ...
- springboot启动出错,
ssm框架 启动后报错 内容如下 [RMI TCP Connection(2)-127.0.0.1] WARN org.springframework.boot.context.embedded.An ...
- 给Tomcat打开远程debug端口
>cd apache-tomcat-8.5.24 >cd conf >vim catalina.sh 在文件开始处添加: CATALINA_OPTS="-server -X ...
- Tomcat各版本及源码包下载
Tomcat各版本及源码包下载 1.百度 Tomcat 进入官网2.Tomcat 官网地址:http://tomcat.apache.org/3.所有 Tomcat 版本及源码包下载地址:https: ...