CodeForces 25E Test KMP
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
4
abacaba
abaaba
x
11 利用kmp匹配算法,在kmp匹配算法稍加修改就ok,因为这里可以只匹配一部分前缀,不用完全匹配。
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std; char str[1100000];
char s0[1100000],s1[1100000],s2[1100000];
int next[1100000];
void getnextval(char *p)//求一个字符串的next数组,结果保存在全局变量next[]中。
{
int plen=strlen(p);
next[0]=-1;
int k=-1;
int j=0;
while (j<plen-1)
{
if (k==-1||p[k]==p[j])
{
j++;
k++;
if (p[j]!=p[k])
next[j]=k;
else
next[j]=next[k];
}
else
{
k=next[k];
}
}
} int kmp(char *s,char *p)//以s为匹配串,p为模式串进行匹配,返回匹配后p剩下的字符个数。
{
int l;
int i=0;
int j=0;
int slen=strlen(s);
int plen=strlen(p);
getnextval(p);
while (i<slen&&j<plen)
{
if (j==-1||s[i]==p[j])
{
i++;
j++;
}
else
j=next[j];
}
if (j==plen)//完全匹配的情况,一个字符也不剩,放回0。
l=0;
else if (i==slen) //匹配一部分,也就是p的一个前缀和s的一个后缀完全匹配。
l=plen-j; else
l=plen-(slen-i)-1; return l;
} int cat(char *a,char *b,char *c) //假设a串在最前,b串在中间,c串在最后至少需要多长。
{
str[0]='\0';
int la=strlen(a);
int lb=strlen(b);
int lc=strlen(c);
int len=kmp(a,b);
strcat(str,a);
strcat(str,b+lb-len);
len=kmp(str,c);
return strlen(str)+len;
} int main()
{
while (scanf("%s%s%s",s0,s1,s2)!=EOF)
{
int minn;
int l;
//一下分6种情况讨论。
minn=cat(s0,s1,s2); l=cat(s0,s2,s1);
if (l<minn)
minn=l; l=cat(s1,s2,s0);
if (l<minn)
minn=l; l=cat(s1,s0,s2);
if (l<minn)
minn=l; l=cat(s2,s0,s1);
if (l<minn)
minn=l; l=cat(s2,s1,s0);
if (l<minn)
minn=l; cout <<minn<<endl;
}
return 0;
}
对于kmp的学习推荐博客:http://blog.csdn.net/v_july_v/article/details/7041827,讲的很好。
CodeForces 25E Test KMP的更多相关文章
- Codeforces 25E Test 【Hash】
Codeforces 25E Test E. Test Sometimes it is hard to prepare tests for programming problems. Now Bob ...
- codeforces 631D. Messenger kmp
题目链接 首先想到kmp, 和普通的不一样的是,中间部分严格相等, 头和尾的字符相等但是数量可以不相等. 所以应该把子串的头和尾先去掉,然后对剩下的部分进行kmp. 子串长度为1或2要特别讨论. 不要 ...
- CODEFORCES 25E Test
题意 三个字符串,找一个字符串(它的子串含有以上三个字符串)使它的长度最短,输出此字符串的长度. 题解 先枚举字符串排列,直接KMP两两匹配,拼接即可...答案取最小值.. 常数巨大的丑陋代码 # i ...
- Codeforces 126B. Password (KMP)
<题目链接> 题目大意:给定一个字符串,从中找出一个前.中.后缀最长公共子串("中"代表着既不是前缀,也不是后缀的部分). 解题分析:本题依然是利用了KMP中next数 ...
- Codeforces 126B(kmp)
要点 头尾的最长相同只要一个kmp即可得,于是处理中间部分 扫一遍记录一下前缀的每个位置是否存在一个中间串跟它相同,见代码 如果当前没有,接着用Next数组去一找即可 #include <cst ...
- Codeforces 1163D(kmp、dp)
要点 \(dp[i][j][k]\)表示主串已经到第\(i\)位时,\(s\)匹配在\(j\)位.\(t\)匹配在\(k\)位的最大得分 本来就要试填一层循环,如果转移也写在循环里的化复杂度承受不了, ...
- Codeforces 625B【KMP】
题意就是一个串在另一个串出现几次,但是字符不能重复匹配, 比如aaaaaaa aaaa的答案是1 思路: 本来写了个暴力过的,然后觉得KMP改改就好了,就让队友打了一个: #include < ...
- Codeforces 1163D DP + KMP
题意:给你一个字符串s,以及两个字符串s1,s2.s中有些位置是*,意思是可以随便填字母,s的子串中如果出现一次s1,就加一分,如果出现一次s2,就减一分.问这个字符串s最多可以得多少分? 思路: 设 ...
- [codeforces] 25E Test || hash
原题 给你三个字符串,找一个字符串(它的子串含有以上三个字符串),输出此字符串的长度. 先暴力判断是否有包含,消减需要匹配的串的数量.因为只有三个字符串,所以暴力枚举三个串的位置关系,对三个串跑好哈希 ...
随机推荐
- Android原理View、ViewGroup
Android的UI界面都是由View和ViewGroup及其派生类组合而成的.其中,View是所有UI组件的基类,而ViewGroup是容纳这些组件的容器,其本身也是从View派生出来的.Andro ...
- 非索引列上的统计 <第二篇>
非索引列上的统计 有时候,可能在连接或过滤条件中的列上没有索引.即使对这种非索引列,如果查询优化器知道这些列的数据分布(统计),它也很可能做出最佳的选择. 除了索引上的统计,SQL Server可以在 ...
- 使用OpenSSL生成证书
使用OpenSSL生成证书 下载安装openssl,进入/bin/下面,执行命令(把ssl目录下的openssl.cnf 拷贝到bin目录下)1.首先要生成服务器端的私钥(key文件):openssl ...
- Min and Max
Min and Max 需要处理不同数据类型; 另外*args, 表示的是位置参数, *kwargs表示的是key参数, args的类型为tuple类型, 参数为min(3, 2)时, args为(3 ...
- eclipse php 开发环境配置
一般常用的是eclipse+pdt.我是直接下载的Eclipse for php :http://www.eclipse.org/downloads/packages/eclipse-php-deve ...
- hadoop 1.2.1 安装步骤 伪分布式
最近在系统的学习hadoop 课程第一步是安装hadoop1.x,具体安装步骤如下: 一.系统安装 本文使用centos6.5安装,具体安装步骤省略 二.jdk安装 下载jdk1.7.0_51解压,在 ...
- haproxy简单负载均衡搭建
最近对负载均衡进行搭建具体方法如下: haproxy 修改部分(haproxy-cfg.cfg) global daemon maxconn 4500 defaults mode http timeo ...
- SQL server 中 COUNT DISTINCT 函数
目的:统计去重后表中所有项总和. 直观想法: SELECT COUNT(DISTINCT *) FROM [tablename] 结果是:语法错误. 事实上,我们可以一同使用 DISTINCT 和 C ...
- JavaScript 基础二
JavaScript 事件处理程序就是一组语句,在事件(如点击鼠标或移动鼠标等)发生时执行 ●当事件之间互相影响时,需要有个先后顺序,这时我们声明一个Bool值来做约束 浏览对象: window 对象 ...
- 【Oracle】逻辑结构(TableSpace→Segment→Extent→Block)
一.逻辑体系结构图 二.逻辑结构图组成介绍 从上表能够看出,一个数据库是由多个表空间(tablespace)组成,一个表空间又由多个段(segment)组成,一个段又由多个区(extent)组成 ...