用KMP里面的next数组即可,原理就是next数组的原理
#include<cstdio> #include<cstdlib> #include<algorithm> #include<cstring> #include<vector> #define MAXN 1000000+10 #define ll long long using namespace std; char s[MAXN]; int n; int nxt[MAXN]; void solve(){ scanf("%s",s+1); memset(nxt,0,sizeof(nxt)); nxt[1]=0; int k=0; for(int i=2;i<=n;i++){ while(k&&s[k+1]!=s[i])k=nxt[k]; if(s[k+1]==s[i])k++; nxt[i]=k; if(nxt[i]&&i%(i-nxt[i])==0){ printf("%d %d ",i,i/(i-nxt[i])); } } } int main() { // freopen("data.in","r",stdin); int T=0; while(1){ scanf("%d",&n); if(!n)break; printf("Test case #%d ",++T); solve(); printf(" "); } return 0; }