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
题意:给出一个周期字符串,求出其中子字符串循环的次数。
把第一个子字符串作为模式,求出长度,再进行计算即可。
#include <cstdio>
#include <cstring>
char str[1000005];
int tar[1000005];
int main()
{
while (scanf("%s", str) != EOF)
{
if (strcmp(str, ".") == 0)
break;
int len = strlen(str);
tar[0] = 0;
int cur = -1;
for (int i = 1; i < len; ++i)
{
while (cur > 0 && str[cur+1] != str[i])
cur = -1;
if (str[cur+1] == str[i])
cur++;
tar[i] = cur;
}
if (len % (len - tar[len - 1] -1) == 0)
printf("%d\n", len / (len - tar[len - 1] - 1));
else
printf("1\n");
}
return 0;
}