标签:kmp算法..
#include<iostream> #include<string.h> #include<stdio.h> #define N 1000010 using namespace std; char s[N]; int next[N]; void getnext(char s[]) { int j=-1,i=0,len; next[0]=-1; len=strlen(s); while(i<=len) { if(j==-1||s[i]==s[j]) { ++i; ++j; next[i]=j; } else j=next[j]; } } int main() { int j=1,i,n; while(cin>>n) { if(n==0) { break; } else cin>>s; getnext(s); cout<<"Test case #"<<j<<endl; j++; for(i=2;i<=n;i++) { if(i%(i-next[i])==0 && i!=i-next[i]) cout<<i<<" "<<(i/(i-next[i]))<<endl; } cout<<"\n"; } return 0; }
标签:kmp算法..
原文地址:http://blog.csdn.net/r_misaya/article/details/40273363