141. 周期(KMP)
https://www.acwing.com/problem/content/143/
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 5; char a[maxn]; int n,t; int nxt[maxn]; void calc_next(){ nxt[1] = 0; for(int i = 2, j = 0; i <= n; i++){ while(j > 0 && a[i] != a[j + 1]) j = nxt[j]; if(a[i] == a[j + 1]) j++; nxt[i] = j; } } int main(){ while(cin >> n && n){ scanf("%s",a + 1); calc_next(); printf("Test case #%d\n",++t); for(int i = 2; i <= n; i++){ if(i % (i - nxt[i]) == 0 && i / (i - nxt[i]) > 1){ printf("%d %d\n",i,i / (i - nxt[i])); } } puts(""); } return 0; }