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算法加速.算法高手必会的算法了. 另外 ...
随机推荐
- SDC信息统计分析系统ETL工具的研究与实现[专业:计算机应用技术]
SDC信息统计分析系统ETL工具的研究与实现[专业:计算机应用技术] http://www.docin.com/p-265530271.html
- Android - Base64
Android 将图片转换为Base64 public void convertToBase64(View view) throws IOException { //获取ImageView的图片 Bi ...
- linux 输入设备驱动
<输入子系统简介> a:背景 内核的输入子系统是对“分散的”,“多种不同类别”的输入设备(键盘,鼠标,跟踪杆,触摸屏,加速度计等)进行“统一处理”的驱动程序.具有如下特点: a-1:统一各 ...
- 洛谷.3381.[模板]最小费用最大流(zkw)
题目链接 Update:我好像刚知道多路增广就是zkw费用流.. //1314ms 2.66MB 本题优化明显 #include <queue> #include <cstdio&g ...
- BZOJ 3245: 最快路线 spfa
3245: 最快路线 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=3245 Description 精明的小R每每开车出行总是喜欢走最快 ...
- 接口开发-基于SpringBoot创建基础框架
说到接口开发,能想到的开发语言有很多种,像什么Java啊..NET啊.PHP啊.NodeJS啊,太多可以用.为什么选择Java,究其原因,最后只有一个解释,那就是“学Java的人多,人员招聘范围大,有 ...
- 对一个前端使用AngularJS后端使用ASP.NET Web API项目的理解(2)
chsakell分享了一个前端使用AngularJS,后端使用ASP.NET Web API的项目. 源码: https://github.com/chsakell/spa-webapi-angula ...
- 总结ASP.NET MVC视图页使用jQuery传递异步数据的几种方式
在ASP.NET MVC的视图页向控制器传递异步数据,可能是数组,JavaScript对象,json,表单数据,等等. 关于数据,JavaScript对象有时候和json长得一模一样,有么有? var ...
- 委托、Lambda表达式、事件系列02,什么时候该用委托
假设要找出整型集合中小于5的数. static void Main(string[] args) { IEnumerable<int> source = new List<int&g ...
- value stored to value2 during its initialization is never read
警告:value stored to value2 during its initialization is never read NSMutableArray *datesArray = [[NSM ...