AcWing 141. 周期
原题链接
简单
作者:
wjie
,
2020-07-31 20:01:39
,
所有人可见
,
阅读 434
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 1e6 + 5;
char str[N];
int Next[N], T;
int main()
{
int n;
while (scanf("%d", &n), n)
{
printf("Test case #%d\n", ++T);
memset(Next, 0, sizeof(Next));
scanf("%s", str+1);
int len = strlen(str+1);
for (int j = 0, i = 2; i <= len; ++i)
{
while (j && str[j+1] != str[i]) j = Next[j];
if (str[j+1] == str[i]) j++;
Next[i] = j;
}
for (int i = 2; i <= len; ++i)
{
int t = i - Next[i];
if (t != i && i % t == 0)
{
printf("%d %d\n", i, i/t);
}
}
puts("");
}
return 0;
}