Problem Description
给你一个整数n,接下里有n个字符的字符串,到第i个字符的时候判断周期是否大于2,如果大于2同时是循环字符串,输出相应的长度,和循环了几次。
Sample Input
3
aaa
12
aabaabaabaab
0
Sample Output
Test case #1
2 2
3 3Test case #2
2 2
6 2
9 3
12 4
代码:
#include<cstdio>
#include<cstring>
using namespace std;
char str[1000055];
int next[1000055];
void get_next(char s[])获取next数组
{
int i, j, len = strlen(s);
next[0] = 0;
for(i = 1; i < len; i++)
{
j = next[i - 1];
while(s[i] != s[j] && j > 0)
{
j = next[j - 1];
}
if(s[i] == s[j])
{
next[i] = j + 1;
}
else next[i] = 0;
}
}
int main()
{
int cas = 1;
int i, len, n, j;
while(~scanf("%d", &n) && n)
{
scanf("%s", str);
printf("Test case #%d\n", cas++);
get_next(str);
len = strlen(str);
for(i = 0; i < len; i++)
{
j = i + 1;//字符串长度
//如果满足是循环字符串,同时周期大于1,输出结果
if(j % (j - next[j - 1]) == 0 && (j / (j - next[j - 1])) > 1) {
printf("%d %d\n", j, j / (j - next[j - 1]));
}
}
printf("\n");
}
return 0;
}