Given two strings a and b we define a*b to be their concatenation. For example, if a = “abc” and b = “def” then a*b = “abcdef”. If we think of concatenation as multiplication, exponentiation by a non-negative integer is defined in the normal way: a^0 = “” (the empty string) and a^(n+1) = a*(a^n).
Input
Each test case is a line of input representing s, a string of printable characters. The length of s will be at least 1 and will not exceed 1 million characters. A line containing a period follows the last test case.
Output
For each s you should print the largest n such that s = a^n for some string a.
Sample Input
abcd
aaaa
ababab
.
Sample Output
1
4
3
Hint
This problem has huge input, use scanf instead of cin to avoid time limit exceed.
solution: kmp的next数组
正确性
:next数组记录着字符串匹配过程中失配情况下可以向前多跳几个字符.
next[len]求的是最长公共前后缀的长度
如果有长度小于len的循环节,那么循环节长度微len-nxt[len]
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;int nxt[1000007], len;
char t[1000007];void get_next(){int i=0, j=-1;nxt[0]=-1;while ( i<len ){if ( j==-1||t[i]==t[j] ) i++, j++, nxt[i]=j;else j=nxt[j];}
}int main(){while( scanf("%s", t)!=EOF ){if( t[0]=='.' ) return 0;len=strlen(t);get_next();if( len%(len-nxt[len])==0 ) printf("%d\n", len/(len-nxt[len] ) );else printf("1\n");}return 0;
}