题意:给一串序列,求出序列所有前缀中所有循环次数大于1的前缀长度及该前缀中循环节循环次数
思路:从1到n递增序列长度,若当前长度中存在循环节,且循环次数大于1,则输出
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<vector>
#include<queue>
#include<map>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn = 1e6+5;
const int inf = 0x3f3f3f3f;
int n, fail[maxn], res;
char s[maxn];
void getFail()
{
fail[0] = -1;
int k = -1, j = 0;
while (j < n) {
if (k == -1 || s[j] == s[k]) {
j++;
k++;
fail[j] = k;
}
else
k = fail[k];
}
}
int main()
{
int T = 0;
while (cin >> n && n) {
scanf("%s", &s);
printf("Test case #%d\n", ++T);
getFail();
for (int i = 1; i <= n; i++) {
int k = i-fail[i];
if (i%k == 0 && i/k > 1) {
printf("%d %d\n", i, i/k);
}
}
printf("\n");
}
}