Code:
#include<cstdio> #include<cstring> using namespace std; const int maxn=1000000+5; char S[maxn]; int f[maxn]; void getfail(char p[],int n) { f[0]=0,f[1]=0; for(int i=1;i<n;++i) { int j=f[i]; while(j&&p[i]!=p[j])j=f[j]; f[i+1]=p[i]==p[j]?j+1:0; if(f[i+1]!=0&&(i+1)%(i+1-f[i+1])==0) { printf("%d ",i+1); printf("%d ",(i+1)/(i+1-f[i+1])); } } } int main() { int cnt=0; while(1) { int n;scanf("%d",&n);if(!n)return 0; printf("Test case #%d ",++cnt); scanf("%s",S); getfail(S,n); printf(" "); } return 0; }