#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+7;
char a[maxn],b[maxn];
int nex[maxn];
int n,m;
void pre(){
nex[1]=0;
int i,j=0;
for(i=1;i<m;i++){
while(j>0&&b[i+1]!=b[j+1])
j=nex[j];
if(b[i+1]==b[j+1]) j++;
nex[i+1]=j;
}
}
int main(){
int i,t,len,l,ans;
cin>>t;
while(t--){
scanf("%s",b+1);
m=strlen(b+1);
pre();
len=m-nex[m];
if(len==m) ans=len;//即nex[m]=0 没有周期性
//必须先判断每周期性的,再判断已成环的!!
else if(m%len==0) ans=0;
else ans=len-(m%len);
printf("%d\n",ans);
}
}
hdu3746还是循环节题目。模板都差不多,记住模板对应题型就挺好写的
最新推荐文章于 2021-08-23 23:45:02 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)