poj 4468Spy(kmp算法)
Spy
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 204 Accepted Submission(s): 96
— Sun Tzu
“A spy with insufficient ability really sucks”
— An anonymous general who lost the war
You, a general, following Sun Tzu’s instruction, make heavy use of spies and agents to gain information secretly in order to win the war (and return home to get married, what a flag you set up). However, the so-called “secret message” brought back by your spy, is in fact encrypted, forcing yourself into making deep study of message encryption employed by your enemy.
Finally you found how your enemy encrypts message. The original message, namely s, consists of lowercase Latin alphabets. Then the following steps would be taken:
* Step 1: Let r = s
* Step 2: Remove r’s suffix (may be empty) whose length is less than length of s and append s to r. More precisely, firstly donate r[1...n], s[1...m], then an integer i is chosen, satisfying i ≤ n, n - i < m, and we make our new r = r[1...i] + s[1...m]. This step might be taken for several times or not be taken at all.
What your spy brought back is the encrypted message r, you should solve for the minimal possible length of s (which is enough for your tactical actions).
For each test case there is a single line containing only one string r (The length of r does not exceed 10
5). You may assume that the input contains no more than 2 × 10
6 characters.
Input is terminated by EOF.
aab
abcadabcabcad
aaabbbaaaabbbaa
abcababcd
Case 2: 2
Case 3: 5
Case 4: 6
Case 5: 4
第一次做这种题也是第一次接触kmp算法,感觉很糟糕,这个题有好多不懂的地方,看着解题报告写的,写完了提交78ms,看人家的好多31ms,想自己优化一下,改了半天还是78ms,可能还是因为没有真正理解它吧,不过以后还会继续看的,继续练习,总会学会的
说一下题意,我觉得这个题目还挺难理解的,可能是英语太差了,连着看了好多解题报告才把题目搞懂,悲催。。。定义串B是由串A的若干前缀加在一起再加上串A本身构成的。现在给出串B,求串A的最小长度。我解释一下这个若干前缀的意思,比如A串为abcdef,a、ab、abc、abcd等都可以说是A的前缀,也就是从前往后的某一段字符串
#include<stdio.h>
#include<string.h>
char s[100050],c[100050];
int next[100050],n,cn; void match()
{
int i,j=0,tem,k,a;
for(i=1;i<=n;i++)
{
while(j&&c[j+1]!=s[i])
j=next[j];
if(s[i]==c[j+1])
j++;
if(j==0)
{
for(k=tem;k<=i;k++)
{
c[++cn]=s[k];
a=cn;
if(c[a]==c[next[a-1]+1])
next[a]=next[a-1]+1;
}
tem=i+1;
}
}
else if(j==cn)
tem=i+1;//记录当前s位置的下一个位置
}
cn+=n-tem+1;
} int main()
{
int i,j,cas=1;
while(scanf("%s",s+1)!=EOF)
{
n=strlen(s+1);
memset(c,0,sizeof(c));
memset(next,0,sizeof(next));
c[1]=s[1];
cn=1;
match();
printf("Case %d: %d\n",cas++,cn);
}
return 0;
}
poj 4468Spy(kmp算法)的更多相关文章
- POJ 3461 Oulipo[附KMP算法详细流程讲解]
E - Oulipo Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
- poj 2406:Power Strings(KMP算法,next[]数组的理解)
Power Strings Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 30069 Accepted: 12553 D ...
- poj 2406 Power Strings kmp算法
点击打开链接 Power Strings Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 27368 Accepted: ...
- POJ 3461 Oulipo(字符串匹配,KMP算法)
题意:给出几组数据,每组有字符串W和T,问你W在T中出现几次. 思路:字符串长度很大,用KMP算法. 一开始写的是:调用KMP算法查找W在T中是否匹配,若匹配,则个数+1.则接下来T的索引移动相应的距 ...
- [POJ] 3461 Oulipo [KMP算法]
Oulipo Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 23667 Accepted: 9492 Descripti ...
- LA 3026 && POJ 1961 Period (KMP算法)
题意:给定一个长度为n字符串s,求它每个前缀的最短循环节.也就是对于每个i(2<=i<=n),求一个最大整数k>1(如果存在),使得s的前i个字符组成的前缀是某个字符串重复得k次得到 ...
- poj 3461 - Oulipo 经典kmp算法问题
2017-08-13 19:31:47 writer:pprp 对kmp算法有了大概的了解以后,虽然还不够深入,但是已经可以写出来代码,(可以说是背会了) 所以这道题就作为一个模板,为大家使用吧. 题 ...
- POJ 1961 Period KMP算法之next数组的应用
题意:给一个长度为n的字符串,如果它长度为l(2 <= l <= n)的前缀部分是由一些相同的字符串相接而成,输出前缀的长度l和长度为l时字符串重复的最大次数. 例如字符串为: aaaba ...
- POJ 3461 Oulipo KMP算法题解
本题就是给出非常多对字符串,然后问一个字符串在另外一个字符串出现的次数. 就是所谓的Strstr函数啦. Leetcode有这道差点儿一模一样的题目. 使用KMP算法加速.算法高手必会的算法了. 另外 ...
随机推荐
- 干货: 可视化项目实战经验分享,轻松玩转 Bokeh (建议收藏)
作者 | Will Koehrsen 翻译 | Lemon 译文出品 | Python数据之道 (ID:PyDataRoad) 本文通过一个项目案例,详细的介绍了如何从 Bokeh 基础到构建 Bok ...
- [AHOI2013]作业
[AHOI2013]作业 题目大意: 给定一个长度为\(n(n\le10^5)\)的数列\(A(1\le A_i\le n)\).\(m(m\le10^6)\)次询问,每次询问区间\([l,r]\)内 ...
- Android笔记(一):this 的表示范围和 Context
this 的表示范围 this 指的是它所在的直接所在的类. 例如: public class MyClass{ int num; public MyClass(int num){ this.num ...
- mobile开发备忘
css -webkit-tap-highlight-color webkit点击时会反色,可以清楚 -webkit-appearance 设为none时自带组建样式清除
- LinkedIn实时低延迟数据抓取系统Databus开源
http://www.infoq.com/cn/news/2013/03/linkedin-databus
- 给Eclipse安装eUML2插件以及可能出现的依赖错误解决方案(转)
eUML2是一款强大的,基于Eclipse应用程序的UML建模工具.开发者可以在UML开发过程中将模型转化为Java代码:确保软件质量和减少开发时间. 必备条件 Java runtime 1.5 or ...
- HDU 4123 Bob’s Race(树形DP,rmq)
Bob’s Race Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- STM32F4 -- How to use the DMA burst feature
Bits 15:13 Reserved, must be kept at reset value. Bits 12:8 DBL[4:0]: DMA burst length This 5-bit ve ...
- 各种版本的ST-LINK仿真器
1.ST官方正式出版了两种仿真器:ST-LINK.ST-LINK/V2,其他型号(ST-LINK II,ST-LINK III,…)要么是国内公司生产,要么是开发板自带的:2.在ST官网ST-LINK ...
- Programming 2D Games 读书笔记(第二章)
本意还是想了解DirectX的,由于网上拿不到书的pdf文档,幸好有作者的源代码示例,想完整的看一下,基本的游戏需要的点. 下面直接以代码为例,仅用于帮助自身理解 http://www.progr ...