POJ 2406
1 #include<iostream> 2 #include<stdio.h> 3 #define MAXN 1000001 4 using namespace std; 5 6 char c[MAXN]; 7 int next[MAXN]; 8 9 void give_next(int len) 10 { 11 int i; 12 int j; 13 i = 0; 14 j = -1; 15 next[0] = -1;//next[0]初始化为-1. 16 while(i < len) 17 { 18 if(j == -1 || c[i] == c[j])//从零开始记录next数组 19 { 20 i ++; 21 j ++; 22 next[i] = j; 23 } 24 else 25 j = next[j]; 26 } 27 } 28 29 30 int main() 31 { 32 //freopen("acm.acm","r",stdin); 33 int len; 34 int tem; 35 while(cin>>c) 36 { 37 if(c[0] == '.') 38 break; 39 //cout<<c<<endl; 40 len = strlen(c); 41 give_next(len); 42 tem = len - next[len]; 43 if(len % tem == 0) 44 { 45 cout<<len/tem<<endl; 46 } 47 else 48 cout<<1<<endl; 49 } 50 }