@Chilling
        
        2017-07-23T12:17:14.000000Z
        字数 931
        阅读 1190
    KMP
题目描述
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 abcdefgabcdefgabcdefgabcdefg.... Then I cut the red part: efgabcdefgabcde as string B. From B, you should find out the shortest A.
输入
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.
输出
For each line, output an integer, as described above.
样例输入
bcabcab
efgabcdefgabcde
样例输出
3
7
题意:求一个串的最小循环节。
#include<bits/stdc++.h>using namespace std;const int maxn = 1e6 + 5;char s[maxn];int Next[maxn];void getNext(char s[]){int len = strlen(s);Next[0] = -1;int j = 0, k = -1;while(j < len){if(k == -1 || s[j] == s[k])Next[++j] = ++k;elsek = Next[k];}}int main(){while(scanf("%s", s) != EOF){int l = strlen(s);getNext(s);printf("%d\n", l - Next[l]);}return 0;}
