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.
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.
题目大意 : 输入一个字符串, 输出该字符串可以最多由多少个字符串拼得
思路 : 没啥难度, KMP中getnext表示的就是该下标之前, 最大前缀与后缀相同的长度, 那么运行到i的时候, i - nxt[i]就是循环体的长度了, 所以最大的一定是看最后一个字符。
AC代码 :
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
const int maxn = 1e7 + 5;
char str[maxn];
int nxt[maxn], len_;
void getnext() {
nxt[0] = -1;
int i = 0, j = -1;
while (i < len_) {
if (j == -1 || str[i] == str[j])
nxt[++i] = ++j;
else
j = nxt[j];
}
}
int main()
{
while (scanf("%s", str) && strcmp(str, ".") != 0) {
memset(nxt, 0, sizeof(nxt));
len_ = strlen(str);
getnext();
if (len_ % (len_ - nxt[len_]) == 0) cout << len_ / (len_ - nxt[len_]) <<endl;
else cout << 1 << endl;
}
return 0;
}