KMP中next[ ]数组有一个性质:
若一个字符串s[ ]为循环串时(例如abcabcabc),那么它的next[ ]数组满足下列性质:
1.len%(len-p[len])==0
2.len/(len-p[len]) 就是该字符串的循环次数 //注意 该题目中若len/(len-p[len])==1,则非循环串,不可取
for(i=2;i<=n;i++){
if(i%(i-s[i])==0&&i/(i-p[i])!=1){
}
}
代码
#include "iostream"
#include "algorithm"
#include "cstring"
#include "iomanip"
#include "stdio.h"
using namespace std;
char s[1000005];
int nextt[1000005];
void getnext(int n){
int i=0,j=-1;
nextt[0]=-1;
while(i<n){
if(j==-1||s[i]==s[j]){
i++;
j++;
nextt[i]=j;
}
else j=nextt[j];
}
}
int main(){
int n,t=0;
std::ios::sync_with_stdio(false);
while(cin>>n&&n!=0){
memset(s,0,sizeof(s));
cin>>s;
printf("Test case #%d\n",++t);
getnext(n);
for(int i=2;i<=n;i++){
if(i%(i-nextt[i])==0&&i/(i-nextt[i])!=1)
printf("%d %d\n",i,i/(i-nextt[i]));
}
printf("\n");
}
return 0;
}