版权声明:本文为博主原创作品, 转载请注明出处! https://blog.csdn.net/solider98/article/details/83819314
思路分析:
在本人之前的博文中详细讨论过字符串循环元与next数组的关系, 直接应用相关结论即可, 下面给出AC代码:
//POJ1961_Period
#include <iostream>
#include <cstdio>
using namespace std;
const int MAX = 1e6 + 5;
char s[MAX]; int len;
int nex[MAX];
int main(){
int sn = 0;
while(++sn, scanf("%d", &len), len){
scanf("%s", s + 1), cout << "Test case #" << sn << endl;
//计算next数组
nex[1] = 0;
for(int i = 2; i <= len; ++i){
int t = nex[i - 1]; while(t && s[t + 1] != s[i]) t = nex[t];
if(t > 0) nex[i] = t + 1;
else if(s[1] == s[i]) nex[i] = 1; else nex[i] = 0;
if(nex[i] > 0 && i % (i - nex[i]) == 0) cout << i << " " << i / (i - nex[i]) << endl;
}
cout << endl;
}
return 0;
}