您的位置:首页 > 其它

POJ 2406 - Power String

2016-08-30 15:55 253 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ACM POJ