HDU 4468

来源:互联网 发布:listview加载网络图片 编辑:程序博客网 时间:2024/04/28 18:59


Spy

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


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 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


事实上r的第一个字母一定是s的第一个字母,刚开始是令s = r[0],然后向后做KMP匹配,如果发现当前字符可以接收(包括匹配指针失败回退若干次后能够接收),就说明当前字符可以作为当前的s的某个前缀中的一部分,接收之;若当前字符令匹配失败,说明无法利用s的某一前缀来覆盖该字符,就将上一次完全匹配时的位置到当前位置这一段的字符加到s上。


#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <string>#include <map>#include <queue>#include <stack>using namespace std;#define INF 1000000000#define N 2000100#define ll long longchar str[N], pat[N];int nxt[N];void getNext(int pos){    int p = nxt[pos-1];    for(; p != -1 && pat[p+1] != pat[pos]; p = nxt[p]);    p = pat[p+1] == pat[pos] ? p+1 : -1;    nxt[pos] = p;}int main(){    //freopen("C:\\Users\\zfh\\Desktop\\in.txt", "r", stdin);    int cas = 0;    while(scanf(" %s", str) != -1)    {        int p = 0, len = 0, maxLen = strlen(str), lastPos = 0;        pat[len++] = str[0];        nxt[0] = -1;        for(int i = 1; i < maxLen; i++)        {            for(; p != 0 && pat[p] != str[i]; p = nxt[p-1]+1);            if(pat[p] != str[i])            {                for(int j = lastPos+1; j <= i; j++)                {                    pat[len++] = str[j];                    getNext(len-1);                }                p = 0;                lastPos = i;            }            else            {                p++;                if(p == len)                {                    p = 0;                    lastPos = i;                }            }        }        len += maxLen-lastPos-1;        printf("Case %d: %d\n", ++cas, len);    }return 0;}


0 0
原创粉丝点击