题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3746
用nxt数组求最短循环节
最短循环节为
T=len−nxt[len]
其中len为字符串长度,字符串是从0开始存的
题目要求凑成至少两个的循环要补多少个,那么算下循环节搞一搞就可以了
#include <stdio.h>
#include<cstring>
using namespace std;
char b[100010];
int nxt[100010];
int lb;
void gnx(){
nxt[0]=-1;
int i=0,j=-1;
while(i<lb){
if(j==-1||b[i]==b[j])nxt[++i]=++j;
else j=nxt[j];
}
}
int main(void)
{
int t;
scanf("%d",&t);
while(t--){
scanf("%s",b);
lb=strlen(b);
gnx();
int T=lb-nxt[lb];
if(T==lb){printf("%d\n",lb);continue;}
printf("%d\n",(T-(lb%T))%T);
}
return 0;
}