hdu 4468 spy(KMP应用,5级)
来源:互联网 发布:陈道明 传承者 知乎 编辑:程序博客网 时间:2024/05/10 01:04
Spy
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 285 Accepted Submission(s): 136
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).
— 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 105). You may assume that the input contains no more than 2 × 106 characters.
Input is terminated by EOF.
For each test case there is a single line containing only one string r (The length of r does not exceed 105). You may assume that the input contains no more than 2 × 106 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
abcaababcadabcabcadaaabbbaaaabbbaaabcababcd
Sample Output
Case 1: 3Case 2: 2Case 3: 5Case 4: 6Case 5: 4
Source
2012 Asia Chengdu Regional Contest
Recommend
liuyiding
#include<cstdio>#include<iostream>#include<cstring>#define FOR(i,a,b) for(int i=a;i<=b;++i)#define clr(f,z) memset(f,z,sizeof(f))using namespace std;const int mm=1e5+9;char s[mm],t[mm];int f[mm];int main(){ int len,pos,last,cas=0; while(~scanf("%s",s+1)) { len=strlen(s+1); pos=1;clr(t,0); t[1]=s[1]; int j=0;last=0; FOR(i,1,len) { while(j&&s[i]!=t[j+1])j=f[j];///正常匹配 if(s[i]==t[j+1])++j; if(!j)///都没有可匹配的。就加上这一段 { int p=f[pos]; FOR(k,last+1,i) { t[++pos]=s[k]; while(p&&t[pos]!=t[p+1])p=f[p];///找匹配 if(t[pos]==t[p+1])f[pos]=p+1; else f[pos]=0; } j=pos; } if(j==pos)last=i; } printf("Case %d: %d\n", ++cas,len+pos-last); }}
- hdu 4468 spy(KMP应用,5级)
- hdu-4468-Spy-KMP+贪心
- hdu 4468 spy kmp+dp
- HDOJ 4468 Spy KMP
- HDU 4468 Spy && 动态KMP && 2012 Asia Chengdu Regional Contest
- poj 4468Spy(kmp算法)
- HDU 4468 Spy KMP + 贪心 (2012成都区域赛E题)
- hdu(kmp的应用)
- hdu4468 Spy kmp
- hdu4468 spy KMP
- hdu4468 Spy,KMP
- hdu oj Period (kmp的应用)
- hdu 3336(KMP算法的应用)
- HDU 1358 Period(KMP算法应用)
- 【KMP应用】HDU 1711
- hdu 4274 Spy's Work(水题)
- HDU 3336 kmp的应用
- HDU 2594 kmp简单应用
- 旋转字符串的三种算法
- 计算机网络知识
- 2440init代码分析(2)
- 2013年google校园招聘笔试题及答案
- jvectormap 总结
- hdu 4468 spy(KMP应用,5级)
- android 读取文件生成Bitmap的几种方式
- HDU 4325 Flowers (线段树+离散化)
- HDU 1035 - Robot Motion
- 【分享】熟练的Java程序员应该掌握哪些技术?
- vb.net 将数据库中的内容显示到comboBox中的代码
- 程序员面试之道(《程序员面试笔试宝典》)之如何与企业进行待遇谈判?
- ServletContext总结
- java集合类之ArrayList