POJ 2406 Power Strings(字符串的最小循环节)

时间:2024-01-21 13:45:59

题目链接:http://poj.org/problem?id=2406

题意:确定字符串最多是多少个相同的字串重复连接而成的

思路:关键是找到字符串的最小循环节

code:

 #include <cstdio>
#include <cstring>
const int MAXN = ;
char s[MAXN];
int next[MAXN];
void GetNext()
{
int len = strlen(s);
int i = ;
int j = -;
next[] = -;
while (i < len)
{
if (- == j || s[i] == s[j]) next[++i] = ++j;
else j = next[j];
}
} int main()
{
while (scanf("%s", s), s[] != '.')
{
int len = strlen(s);
GetNext();
if (len % (len - next[len]) == ) printf("%d\n", len / (len - next[len]));
else printf("1\n");
}
return ;
}