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& ...
随机推荐
- VNC 设置分辨率 --TigerVNC
之前写过如何设置vnc https://www.cnblogs.com/jinanxiaolaohu/p/9110002.html 如果想设置vnc的分辨率的话 也很简单 如图示: vncserver ...
- Heavy Transportation POJ 1797 最短路变形
Heavy Transportation POJ 1797 最短路变形 题意 原题链接 题意大体就是说在一个地图上,有n个城市,编号从1 2 3 ... n,m条路,每条路都有相应的承重能力,然后让你 ...
- zookeeperAPI的常用方法
zookeeper支持三种部署方式: 1 单机 2 集群 3 伪集群 删除节点 zkClient.delete(path);
- JS基础_字面量和变量
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- O020、理解 Glance
参考https://www.cnblogs.com/CloudMan6/p/5384923.html OpenStack 由 Glance 提供 Image 服务. 理解 Glance ...
- linux入门到高级
目 录 1. Linux入门篇.................................................................................... ...
- Hadoop环境安装和集群创建
虚拟机使用vmware,vmware可以直接百度下载安装 秘钥也能百度到 安装很简单 CentOS 7下载: 进入官网 https://www.centos.org/download/ 这里有三种 第 ...
- 计算机网络:这是一份全面 & 详细 的TCP协议学习指南
原文链接:blog.csdn.net 用这个媒体播放器组件,实时互动时也可共同观看本地视频juejin.im 前言 计算机网络基础 该是程序猿需掌握的知识,但往往会被忽略 今天,我将详细讲解计算机网络 ...
- Exited too quickly (process log may have details)-配置问题
在配置supervisor的时候,提示Exited too quickly (process log may have details),这个时候一脸懵逼,啥回事,执行太快了???
- easyui datagrid连续删除问题
如果在datagrid中直接将index传给easyui自带的deletRow方法来删除当前点击行,一开始并没有问题,但是当连续删除的时候就或出问题了. 原因是datagrid行是根据datagrid ...