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
原题 给你三个字符串,找一个字符串(它的子串含有以上三个字符串),输出此字符串的长度. 先暴力判断是否有包含,消减需要匹配的串的数量.因为只有三个字符串,所以暴力枚举三个串的位置关系,对三个串跑好哈希 ...
随机推荐
- File类详解
一.File类: File类时io包中唯一代表磁盘文件本身的对象.File类定义了一些与平台无关的方法来操作文件,可以通过调用File类中的方法,实现创建.删除.重命名文件等. File类的对象主要用 ...
- Android 开发中关于layoutinflater
Inflater英文意思是膨胀,在Android中应该是扩展的意思吧. LayoutInflater的作用类似于 findViewById(),不同点是LayoutInflater是用来找layout ...
- LeetCode_Flatten Binary Tree to Linked List
Given a binary tree, flatten it to a linked list in-place. For example, Given 1 / \ 2 5 / \ \ 3 4 6 ...
- 猎豹上市(猎豹的广告收入中有70%来自BAT三家公司,总收入中有58%来自BAT)
发表日期: 2014 年 5 月 9 日 From 网易专题 文/赵楠 村里那点儿事 猎豹移动上市之夜,我挺激动. 激动除了因为有好朋友在这家公司外,也因为猎豹移动在历史上的几次起承转合非常不易,在巨 ...
- 《Programming WPF》翻译 第9章 6.我们进行到哪里了?
原文:<Programming WPF>翻译 第9章 6.我们进行到哪里了? 只有当任何内嵌控件都没有提供你需要的底层行为时,你将要写一个自定义控件.当你写一个自定义控件,你将要使用到依赖 ...
- (DP)House Robber
题目: You are a professional robber planning to rob houses along a street. Each house has a certain am ...
- Android NDK R9d 安装
NDK是一个工具集,可让您实现您的应用程序使用本机代码的语言,如C和C + +.Android NDK 是在SDK前面又加上了“原生”二字,即Native Development Kit,因此又被Go ...
- xib和Storyboard 创建Cell的方式
xib 方式 .在Cell.h文件中加一个宏 #define cellIdentifier @"customCell" . ViewController中: - (void)vie ...
- ASP.NET中时间的绑定和格式化
1.Eval和Bind的区别 绑定表达式 <%# Eval("字段名") %> <%# Bind("字段名") %> 区别 1.e ...
- Win7 公布网站 HTTP 错误 404.4 - Not Found
NET IIS7.5 创建网站时,假设发现下面错误,而且 默认网站訪问没有问题的话, 能够尝试,进入 处理程序映射 右键恢复为父级,有可能会有意想不到的 惊喜. 我的问题就是这样解决的. 出现这 ...