AcWing 141. 周期
原题链接
简单
作者:
梅
,
2021-03-30 13:57:55
,
所有人可见
,
阅读 374
C++ 代码
#include<iostream>
using namespace std;
const int N = 1000010;
int n, m;
char s[N], p[N];
int ne[N];
int main(){
int e = 0;
while(cin >> n, n){
printf("Test case #%d\n", ++e);
cin >> s + 1;
for(int i = 2, j = 0; i <= n; ++i){
while(j && s[i] != s[j + 1]){
j = ne[j];
}
if(s[i] == s[j + 1]) ++j;
ne[i] = j;
}
//循环节=i-ne[i](next数组的定义 print一遍ne能看清楚)
for(int i = 1; i <= n; ++i){
if(ne[i] != 0 && i % (i - ne[i]) == 0)
cout << i << " " << i /(i - ne[i]) << endl;
}
cout << endl;
// for(int i = 1; i <= n; ++i) cout << ne[i] << " ";
// cout << endl;
}
return 0;
}