一年前做的.
求一个字符串由多少个循环节构成.
KMP.
结论:若(n \% (n-next[n])
ot= 0)则不是由循环节构成的.
否则,答案为(n/(n-next[n])).
code
#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#define MogeKo qwq
using namespace std;
#include<string>
const int maxn = 1e6+10;
int n,f[maxn];
char s[maxn];
int main() {
while(1) {
scanf("%s",s);
if(s[0] == '.')break;
n = strlen(s);
f[0] = f[1] = 0;
for(int i = 1; i < n; i++) {
int j = f[i];
while(j && s[i]!=s[j]) j = f[j];
if(s[i] == s[j]) f[i+1] = j+1;
else f[i+1] = 0;
}
if(n%(n-f[n])!=0) printf("1
");
else printf("%d
",n/(n-f[n]));
}
return 0;
}