循环节变种。使之成为循环
#include <cstdio>
#include <cstring>
using namespace std;
const int MAX_N = 1000100;
#define sci(num) scanf("%d",&num)
char
str[MAX_N],ps[MAX_N];
int nxt[MAX_N];
int lents,lenps;
void kmp_pre(
char
* t) {
nxt[0] = -1;
for (int i = 0,k = -1;i < lents;) {
if (k == -1 || t[i] == t[k]) {
nxt[++i] = ++k;
} else {
k = nxt[k];
}
}
}
int main() {
int cases;
sci(cases);
for (int cs =1;cs <= cases;cs++){
scanf("%s",str);
lents = strlen(str);
kmp_pre(str);
int ans = lents;
if (nxt[lents] && lents % (lents - nxt[lents]) == 0) {
ans = 0;
} else {
ans = lents - nxt[lents] - nxt[lents] % (lents - nxt[lents]);
}
printf("%d\n",ans);
}
return 0;
}