
Description
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.
题目大意:给一个字符串,问这个字符串是否能由另一个字符串重复R次得到,求R的最大值。
思路:对于一个字符串,如abcd abcd abcd,由长度为4的字符串abcd重复3次得到,那么必然有原字符串的前八位等于后八位。
也就是说,对于某个字符串S,长度为N,由长度为n的字符串s重复R次得到,当R≥2时必然有S[1..N-n]=S[n+1..N]。
那么对于KMP算法来说,就有fail[N]=N-n。此时n肯定已经是最小的了。
然后只需要判断N是否n的倍数,是则输出N/n即可。否则输出1。
代码(157MS):
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
using namespace std; const int MAXN = ; void getFail(char *P, int m, int f[]) {
f[] = f[] = ;
for(int i = ; i < m; ++i) {
int j = f[i];
while(j && P[i] != P[j]) j = f[j];
f[i + ] = (P[i] == P[j] ? j + : );
}
} char s[MAXN];
int fail[MAXN], n; int main() {
while(scanf("%s", s) != EOF) {
if(*s == '.') break;
n = strlen(s);
getFail(s, n, fail);
if(n % (n - fail[n]) == ) printf("%d\n", n / (n - fail[n]));
else puts("");
}
}