#include<stdio.h> #include<string.h> #include<iostream> #include<math.h> #include<algorithm> #include<stdlib.h> #include<queue> #include<stack> using namespace std; #define N 1000010 char s[N]; int f[N]; void getFail(char *P){ int i = 0, j = -1; f[0] = -1; while(P[i]){ if(j == -1 || P[i] == P[j]) f[++i] = ++j; else j = f[j]; } } int main(){ while(~scanf("%s",s),s[0] != ‘.‘){ getFail(s); int len = strlen(s); int pos = len - f[len]; int ans; if(len % pos)ans = 1; else ans = len /pos; printf("%d\n", ans); } return 0; }
原文:http://blog.csdn.net/acmmmm/article/details/18889477