http://poj.org/problem?id=1961
此题直接上代码:
#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<cmath> #include<queue> #include<algorithm> #include<set> using namespace std; const int N=1000010; char s[N]; int next[N]; int ans[N]; void findnext(int n) { int j=-1; int i=0; next[0]=-1; while(i<n) { if(j==-1||s[i]==s[j]) { ++j;++i;next[i]=j; }else { j=next[j]; } } } int main() { int n; int w=0; while(scanf("%d",&n)!=EOF,n) { ++w; getchar(); gets(s); findnext(n); printf("Test case #%d\n",w); for(int i=2;i<=n;++i) { int l=i-next[i]; if(next[i]>=i/2&&i%l==0) { printf("%d %d\n",i,i/l); } } printf("\n"); } return 0; }