AcWing 141. 周期
原题链接
简单
作者:
落拓Leisure
,
2024-04-15 20:31:40
,
所有人可见
,
阅读 7
#include <iostream>
using namespace std;
const int N = 1000010;
int n, cnt;
char p[N];
int ne[N];
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
while (cin >> n, n)
{
cin >> p + 1;
for (int i = 2, j = 0; i <= n; i ++ )
{
while (j && p[i] != p[j + 1]) j = ne[j];
if (p[i] == p[j + 1]) j ++ ;
ne[i] = j;
}
cout << "Test case #" << ++ cnt << endl;
for (int i = 1; i <= n; i ++ )
if (ne[i] && i % (i - ne[i]) == 0)
cout << i << ' ' << i / (i - ne[i]) << endl;
cout << endl;
}
return 0;
}