求最循环节的个数:
#include "iostream"
#include "algorithm"
#include "cstring"
#include "cstdio"
#include "iomanip"
using namespace std;
string a;
int nextt[100005];
void getnext(){
int i=0,j=-1;
nextt[0]=-1;
while(i<a.length()){
if(j==-1||a[i]==a[j]){
i++;
j++;
nextt[i]=j;
}
else
j=nextt[j];
}
}
int main(){
std::ios::sync_with_stdio(false);
int T,n,m;
cin>>T;
while(T--){
cin>>a;
getnext();
int n=a.length()-nextt[a.length()];//循环节长度
int m=a.length();
if(n!=m&&m%n==0)printf("0\n");//如果可以多次循环
else printf("%d\n",n-nextt[m]%n);//取余的作用:abcab,去掉abc
}
return 0;
}
附:求最大的循环节的个数
int ans = 1;
int l=s.length();
if(l%(l-nextt[l])==0)
ans=l/(l-nextt[l]);
cout<<ans<<endl;