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算法加速.算法高手必会的算法了. 另外 ...
随机推荐
- SQL server学习(一)数据库的基本知识、基本操作和基本语法
在软件测试中,数据库是必备知识,假期闲里偷忙,整理了一点学习笔记,共同探讨. 阅读目录 基本知识 数据库发展史 数据库名词 SQL组成 基本操作 登录数据库操作 数据库远程连接操作 数据库分离操作 数 ...
- Django的URL别名
项目的urls.py配置文件 from message.views import getform urlpatterns = [ url(r'^admin/', admin.s ...
- XXX on tree
%了发树上莫队 nlognsqrt(n) // luogu-judger-enable-o2 #include<bits/stdc++.h> using namespace std; in ...
- bzoj 3999 线段树区间提取 有序链剖
看错题目了,想成每个城市都可以买一个东西,然后在后面的某个城市卖掉,问最大收益.这个可以类似维护上升序列的方法在O(nlog^3n)的时间复杂度内搞定 这道题用到的一些方法: 1. 可以将有关的线段提 ...
- android防止按钮连续点击方案之AOP
转载请标明出处http://www.cnblogs.com/yxx123/p/6675567.html 防止连续点击的实现方式有很多种,比如,在所有的onclick里面加上防多次点击的代码,或者定义一 ...
- px 与 dp, sp换算公式?(转)
PPI = Pixels per inch,每英寸上的像素数,即 "像素密度" xhdpi: 2.0 hdpi: 1.5 mdpi: 1.0 (baseline) ldpi: 0. ...
- C#中执行Dos命令
//dosCommand Dos命令语句 public string Execute(string dosCommand) { ); } /// <summary> /// 执行DOS命令 ...
- Chilkat----开源站点之VS2010 CKMailMan一个很好的邮件发送开源开发包
Chilkat 是一个很好的开源站点,有各种开源库. 开发语言主要有Classic ASP •C • C++ • C# • Delphi ActiveX • Delphi DLL • Visual F ...
- Revit API过滤元素类别(FamilySymbol与FamilyInstance)
仅OfCategory()过滤的元素包含系统FamilySymbolOfClass(typeof(FamilyInstance))过滤出来文档中族实例. ; ; ; ...
- 面试题07_用两个栈实现队列——剑指offer系列
题目描写叙述: 用两个栈实现一个队列. 队列的声明例如以下,请实现它的两个函数appendTail 和 deleteHead.分别完毕在队列尾部插入结点和在队列头部删除结点的功能. 解题思路: 栈的特 ...