Spy

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 204    Accepted Submission(s): 96

Problem Description
“Be subtle! Be subtle! And use your spies for every kind of business. ”

— 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).
 
Input
There are several test cases.

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.
 
Output
For each test case, output one line “Case X: Y” where X is the test case number (starting from 1) and Y is the desired answer.

 
Sample Input
abc
aab
abcadabcabcad
aaabbbaaaabbbaa
abcababcd
 
Sample Output
Case 1: 3
Case 2: 2
Case 3: 5
Case 4: 6
Case 5: 4
 
Source
 

第一次做这种题也是第一次接触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;
}

最新文章

  1. tomcat 内存问题 xms xmx permsize maxPermsize
  2. css —— 图片环绕+首行缩进
  3. Charles辅助调试接口
  4. linux笔记_20150825_linux下的软件工具唠叨下
  5. JavaFX前言
  6. Chrome浏览器下调试SASS
  7. C#文件后缀名详解
  8. PC-hosts 的使用 [可使电脑无法正常上网]
  9. Oracle MERGE INTO的使用方法
  10. [LeetCode]题解(python):070-Climbing Stairs
  11. BZOJ 1053 反素数ant
  12. ABP分层架构
  13. 宿主机丢失vmnet0和vmnet8网卡
  14. 打造基于Clang LibTooling的iOS自动打点系统CLAS(三)
  15. HDU 6092 Rikka with Subset
  16. SLIC superpixel算法
  17. JAVA_SE基础——51.内部类
  18. OpenCV 轮廓检测
  19. UWP Acrylic Material
  20. 『Kruscal重构树 Exkruscal』

热门文章

  1. Java基础组件快速入门
  2. SpringMVC框架05——拦截器
  3. [转]Splay算法
  4. Android处理各种触摸事件
  5. codevs 1086 栈 2003年NOIP全国联赛普及组
  6. codeforces 596E Wilbur and Strings
  7. Apache Kafka —一个不同的消息系统
  8. [Asp.net core]使用Polly网络请求异常重试
  9. CreateJs入门必知必会
  10. 在ASP.NET Web API中使用OData的单例模式