poj 4468Spy(kmp算法)
生活随笔
收集整理的這篇文章主要介紹了
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).
?
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 2012 Asia Chengdu Regional Contest第一次做這種題也是第一次接觸kmp算法,感覺很糟糕,這個題有好多不懂的地方,看著解題報告寫的,寫完了提交78ms,看人家的好多31ms,想自己優化一下,改了半天還是78ms,可能還是因為沒有真正理解它吧,不過以后還會繼續看的,繼續練習,總會學會的
說一下題意,我覺得這個題目還挺難理解的,可能是英語太差了,連著看了好多解題報告才把題目搞懂,悲催。。。定義串B是由串A的若干前綴加在一起再加上串A本身構成的?,F在給出串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; }?
?
轉載于:https://www.cnblogs.com/xinyuyuanm/p/3212005.html
總結
以上是生活随笔為你收集整理的poj 4468Spy(kmp算法)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PDF417二维条码生成器 C++
- 下一篇: 基于visual Studio2013解