http://poj.org/problem?id=2406
题意 :求最小的重复子串的个数。
思路 :KMP。

#include <string.h> #include <stdio.h> #include <iostream> using namespace std ; const int maxn = 10000000 ; char ch[maxn] ; int next[maxn] ; int main() { while(scanf("%s",ch)!=EOF) { if(ch[0] == '.') break ; int len = strlen(ch) ; int i = 0 , j = -1 ; next[0] = -1 ; while(i < len ) { if(j == -1 || ch[i] == ch[j]) next[++i] = ++j ; else j = next[j] ; } int num ; i = len - j ;//因为j在++,现在减掉之后剩下的一定是循环结的长度 num = (len%i == 0) ? len/i : 1 ; printf("%d ",num) ; } return 0 ; }