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& ...
随机推荐
- job创建之后,不运行
创建job的时候要commit,然后还要看job是否分派进程 加上之后还是没有执行,后来发现 show parameter job_queue_process; 的结果为0,没有为job分配进程,所以 ...
- sql实现同时向主表和子表插入数据方法
使用sql语句实现同时向主表和子表插入数据方法: Oracle: -- oracle创建sequence create sequence SEQ_test minvalue 1 maxvalue 99 ...
- Mysql workbench 字段类型(转载)
转载自:https://blog.csdn.net/j_h_xie/article/details/52924521 项目初始,在使用workbench建表时,字段中有PK,NN,UQ,BIN,UN, ...
- Django中ajax发送post请求,报403错误CSRF验证失败解决办法
今天学习Django框架,用ajax向后台发送post请求,直接报了403错误,说CSRF验证失败:先前用模板的话都是在里面加一个 {% csrf_token %} 就直接搞定了CSRF的问题了:很显 ...
- 大数据学习(1)-shell脚本注意事项
1.变量=值 (例如STR=abc) 不用加引号,但此时空格不再是空格字符,特殊字符可用于转义 2.等号两侧不能有空格 3.变量名称一般习惯为大写 4.双引号和单引号有区别,双引号仅将空格脱意,单引 ...
- oa_mvc_easyui_删除(6)
1.删除列,添加a标签,绑定参数 <a href="javascript:void(0)" class="delete" ids="@newli ...
- java中的异常和处理详细理解
异常是程序中的一些错误,但并不是所有的错误都是异常,并且错误有时候是可以避免的. 比如说,你的代码少了一个分号,那么运行出来结果是提示是错误 java.lang.Error:如果你用System.ou ...
- sqlserver2016 management tool v18
安装完sql server 2016 sp1版本后再安装管理工具v18版本,启动管理工具,启动不起来,自动退出了,没有任何反应. 解决该问题方案: 找到Microsoft.VisualStudio.S ...
- JAVA核心技术--继承(1)
1.继承:向上追溯,对同一批类的抽象,延续和扩展父类的一切信息! 1)关键字:extends 例如,父类是Animal,子类是Dog; eg: public class Dog exte ...
- 问题:tomcat启动后,可以访问主页面,但是无法访问dubbo-admin
原因分析: 直接查看logs中的日志文件,发现一行 [Catalina-utility-1] org.apache.catalina.startup.HostConfig.undeploy Undep ...