题意大概是在该字符串末尾最少添加多少个字符,可以让这个字符串获得重复循环序列。
最小循环节长度L = 字符串的长度sL - Next[sL],循环节循环次数c = sL / L
字符串循环条件:sL % L == 0
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 1e5 + 5;
char p[N];
int next[N], pl;
void get_next(){
int i = 0,j = -1;
next[0] = -1;
while(i < pl)
{
if(j == -1 || p[i] == p[j]) next[++i] = ++j;
else j = next[j];
}
}
int main()
{
int n;
scanf("%d", &n);
getchar();
while(n--){
scanf("%s", p);
pl = strlen(p);
get_next();
int tmp = pl - next[pl];//一个最小循环节的长度
//题意循环次数>1
if(pl % tmp == 0 && pl / tmp >= 2){
printf("0\n");
}
else{
printf("%d\n", tmp - (pl % tmp));//pl % tmp表示循环节中已有多少个字符
}
}
return 0;
}