HUST 1010 The Minimum Length

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. 

InputMultiply 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. 
OutputFor each line, output an integer, as described above.Sample Input

bcabcab
efgabcdefgabcde

Sample Output

3
7

kmp求最小循环节。

代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char str[1000001];
int Next[1000001],len,k;
void findNext() {
    int j = -1,i = 0;
    Next[0] = -1;
    while(i < len) {
        if(j == -1 || str[i] == str[j]) {
            Next[++ i] = ++ j;
        }
        else j = Next[j];
    }
}
int main() {
    while(~scanf("%s",str)) {
        len = strlen(str);
        findNext();///先确立Next数组

        printf("%d
",len - Next[len]);
    }
}
原文地址:https://www.cnblogs.com/8023spz/p/9656025.html