标签:kmp
next数组的应用
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<queue> #include<stack> #include<vector> #define L(x) (x<<1) #define R(x) (x<<1|1) #define MID(x,y) ((x+y)>>1) #define eps 1e-8 using namespace std; #define N 1000005 int len,next[N]; char a[N]; void getfail(char *a) { int i,j; i=0;j=-1; next[0]=-1; while(i<len) { if(j==-1||a[i]==a[j]) { i++; j++; next[i]=j; } else j=next[j]; } } int main() { int i,j,ca=0; while(~scanf("%d",&len),len) { scanf("%s",a); getfail(a); printf("Test case #%d\n",++ca); for(i=2;i<=len;i++) { if(next[i]&&i%(i-next[i])==0) printf("%d %d\n",i,i/(i-next[i])); } printf("\n"); } return 0; }
标签:kmp
原文地址:http://blog.csdn.net/u014737310/article/details/41121201