CodeForeces 25E (kmp)
E. Test
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.
Examples
input
ab
bc
cd
output
4
input
abacaba
abaaba
x
output
11
void getNext(string P)
{
int len=P.length();
Next[0]=0;
for(int i=1;i<len;i++)
{
int k=Next[i-1];
while(P[i]!=P[k]&&k!=0)
k=Next[k-1];
if(P[i]==P[k])
Next[i]=k+1;
else
Next[i]=0;
}<pre name="code" class="html">
}
还有一个更加精简的kmp算法
void getnext(string P )
{
int j = -1, i = 0;
int len=P.length();
next[0] = -1;
while(i < len)
{
if(j == -1 ||P[i] == P[j])
{
i++;
j++;
next[i] = j;
}
else
j = next[j];
} }
这个两个求next数组算法虽然求得的next数组不太一样,但是都可以用于kmp。下面给kmp的算法
int kmp(string T,string P)
{
int pLen=P.length();
int tLen=T.length();
int i=0,j=0;
while(i<pLen&&j<tLen)
{
if(i==-1||P[i]==T[j])
i++,j++;
else
i=next[i];
}
if(i>=pLen) return j-pLen+1;
else return -1;
</pre>接下来看这道题目:题目实际上是要求两字符串头尾想拼接,也即是头尾重合的最大长度。只需要在kmp的基础上稍加改动即可,下面给出AC代码</div><div class="problem-statement" style="margin:0.5em; padding:0px; line-height:1.5em; font-size:1.4rem"></div><div class="problem-statement" style="margin:0.5em; padding:0px; line-height:1.5em; font-size:1.4rem"><pre name="code" class="html">#include <iostream>
#include <string.h>
#include <string>
#include <algorithm>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
using namespace std;
#define MAX 100000
int _next[3][MAX+5];
string a[3];
int b[3][3];
int c[3];
void get_next(string P,int pos )
{
int j = -1, i = 0;
int len=P.length();
_next[pos][0] = -1;
while(i < len)
{
if(j == -1 ||P[i] == P[j])
{
i++;
j++;
_next[pos][i] = j;
}
else
j = _next[pos][j];
} }
int kmp(string T,string P,int pos)
{
int pLen=P.length();
int tLen=T.length();
int i=0,j=0;
while(i<pLen&&j<tLen)
{
if(i==-1||P[i]==T[j])
i++,j++;
else
i=_next[pos][i];
}
//cout<<i<<endl;
return i;
} int main()
{
cin>>a[0]>>a[1]>>a[2];
c[0]=a[0].length();c[1]=a[1].length();c[2]=a[2].length();
memset(_next,0,sizeof(_next));
get_next(a[0],0);get_next(a[1],1);get_next(a[2],2);
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
if(i!=j)
b[i][j]=kmp(a[i],a[j],j);
int ans=0;
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;
if(b[i][j]==c[j])
ans=max(ans,b[i][j]+b[i][k]);
else
ans=max(ans,b[i][j]+b[j][k]);
}
}
}
printf("%d\n",c[0]+c[1]+c[2]-ans);
return 0; }
CodeForeces 25E (kmp)的更多相关文章
- CodeForces 25E Test KMP
Description Sometimes it is hard to prepare tests for programming problems. Now Bob is preparing tes ...
- codeforeces近日题目小结
题目源自codeforeces的三场contest contest/1043+1055+1076 目前都是solved 6/7,都差了最后一题 简单题: contest/1043/E: 先不考虑m个限 ...
- KMP算法求解
// KMP.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> using namespac ...
- 简单有效的kmp算法
以前看过kmp算法,当时接触后总感觉好深奥啊,抱着数据结构的数啃了一中午,最终才大致看懂,后来提起kmp也只剩下“奥,它是做模式匹配的”这点干货.最近有空,翻出来算法导论看看,原来就是这么简单(先不说 ...
- KMP算法
KMP算法是字符串模式匹配当中最经典的算法,原来大二学数据结构的有讲,但是当时只是记住了原理,但不知道代码实现,今天终于是完成了KMP的代码实现.原理KMP的原理其实很简单,给定一个字符串和一个模式串 ...
- 萌新笔记——用KMP算法与Trie字典树实现屏蔽敏感词(UTF-8编码)
前几天写好了字典,又刚好重温了KMP算法,恰逢遇到朋友吐槽最近被和谐的词越来越多了,于是突发奇想,想要自己实现一下敏感词屏蔽. 基本敏感词的屏蔽说起来很简单,只要把字符串中的敏感词替换成"* ...
- [KMP]【学习笔记】
Oulipo Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 36916 Accepted: 14904 Descript ...
- KMP算法实现
链接:http://blog.csdn.net/joylnwang/article/details/6778316 KMP算法是一种很经典的字符串匹配算法,链接中的讲解已经是很明确得了,自己按照其讲解 ...
- KMP专题
1.[HDU 3336]Count the string(KMP+dp) 题意:求给定字符串含前缀的数量,如输入字符串abab,前缀是a.ab.aba.abab,在原字符串中出现的次数分别是2.2.1 ...
随机推荐
- imx6 电容屏参数更改
imx6使用电容屏时需要获取对应的usb的event.其中用到了shell的一些命令.分析如下. # inputCheckLine=`cat /proc/bus/input/devices | gre ...
- 电脑的CPU可直接解读的数据机器码
机器语言(machine language)是一种指令集的体系.这种指令集,称机器码(machine code),是电脑的CPU可直接解读的数据. 机器码有时也被称为原生码(Native Code), ...
- yasm开源汇编器分析
https://www.google.com.hk/search?q=yasm&oq=yasm&aqs=chrome..69i57&sourceid=chrome&es ...
- python 模块之间相互引用
模块层级关系: ----: |->AA.py |->BB.py |->CC.py AA.py from BB import BB class AA: def sub(self, x) ...
- CentOS6.8安装mysql并设置字符集编码
一.安装: 1. 首先以root用户进入系统 2. 输入命令: yum install mysql mysql-server -y 等待安装完成. 3. 启动MySQL,输入命令: /etc/init ...
- java文件读写工具类
依赖jar:commons-io.jar 1.写文件 // by FileUtilsList<String> lines = FileUtils.readLines(file, " ...
- VC++ 6.0开发套件(自己收藏!)
安装镜像ISO VC++ 6.0_SP6_Win7企业版(中英文集成).iso MSDN安装镜像ISO MSDN_Oct_200 ...
- MyException--org.apache.ibatis.exceptions.PersistenceException: ### Error building SqlSession. ###
org.apache.ibatis.exceptions.PersistenceException: ### Error building SqlSession. ### The error may ...
- swift - UIStepper的用法
在网上查看学习资料的时候,看到有这个控件,所以就自己写了下,感觉在某些特定的地方用的还是挺方便的! 不过,个人感觉,局限性太大! 1.初始化(创建个label是为了让大家看到具体的数值) let st ...
- 【RF库Collections测试】Dictionary Should Contain Sub Dictionary
Name:Dictionary Should Contain Sub DictionarySource:Collections <test library>Arguments:[ dict ...