HUST 1010 字符串匹配(最小循环节问题)

来源:互联网 发布:网络诈骗图片大全 编辑:程序博客网 时间:2024/06/05 23:58
There is a string A. The length of A is less than 1,000,000. I rewrite it again and again. Then I got a new string: AAAAAA...... Now I cut it from two different position and get a new string B. Then, give you the string B, can you tell me the length of the shortest possible string A. For example, A="abcdefg". I got abcd efgabcdefgabcdefgabcdefg.... Then I cut the red part: efgabcdefgabcde as string B. From B, you should find out the shortest A.
Input
Multiply Test Cases. For each line there is a string B which contains only lowercase and uppercase charactors. The length of B is no more than 1,000,000.
Output
For each line, output an integer, as described above.
Sample Input
bcabcab
efgabcdefgabcde
Sample Output
3

7

#include <cstdio>#include <cstring>#include <iostream>using namespace std;char array[1000005];int Next[1000005];int len;void GetNext(){int i=0,j=-1;Next[0]=-1;while(i<len){while(j>-1&&array[i]!=array[j])j=Next[j];Next[++i]=++j;}return;}int main(){while(scanf("%s",array)!=EOF){len=strlen(array);GetNext();printf("%d\n",len-Next[len]);}return 0;}
此题注意观察

1 0
原创粉丝点击