Spy
Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 227 accepted submission (s): 107
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 wocould 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 shoshould solve for the minimal possible length of S (which is enough for your tactical actions ).
Inputthere 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.
Outputfor 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
Source2012 Asia
Chengdu Regional Contest
Recommendliuyiding can be easily identified as KMP. How can this problem be solved? We just want to use the mode string to meet its prefix and splice it into the original string, and it is the suffix of the original string. We can ignore the suffix first, we first find that the prefix is satisfied, we first set the first character of the original string, then, we will match the original string. If we find that it does not match, we will add all the places where the last exact match is here to the pattern string until the end. Isn't that the answer? If it is not reversed at the end, you can add a new one. Otherwise, you can directly output it!
# Include <iostream> # include <stdio. h> # include <string. h> using namespace STD; # define maxn 100000 char STR [maxn], pass [maxn]; int next [maxn], passnum, strnum, lastpass; void getnext () {int I, j; next [0] = next [1] = 0; for (I = lastpass, j = 0; I <passnum; I ++) {J = next [I]; while (J & pass [I]! = Pass [J]) {J = next [J];} next [I + 1] = pass [I] = pass [J]? J + 1:0 ;}} int fkmp () {int I, j, last, K; last = 1; for (I = 1, j = 0; I <strnum; I ++ )//?? {While (J & STR [I]! = Pass [J]) {J = next [J];} If (STR [I] = pass [J]) {J ++ ;} if (j = 0) {lastpass = passnum; For (k = last; k <= I; k ++) {pass [passnum ++] = STR [k];} Last = I + 1; pass [passnum] = '\ 0'; getnext (); // printf ("% S % d \ n", pass, I, J, passnum);} If (j = passnum) {last = I + 1; j = 0; // complete} If (last = strnum) return passnum; else return strnum-last + passnum;} int main () {int tcase = 1; while (scanf ("% s", STR )! = EOF) {strnum = strlen (STR); lastpass = 1; passnum = 1; pass [0] = STR [0]; pass [1] = '\ 0 '; printf ("case % d: % d \ n", tcase ++, fkmp ();} return 0 ;}