题意
一串字符,只能在串首和串尾添加字符,最少添加几个字符前后字符串连接起来会是一个循环节组成的串
关于next数组的应用 记下结论
len−next[i]
l
e
n
−
n
e
x
t
[
i
]
为这个串的循环节大小
那么我们最少需要添加的就是循环节大小减去不足循环节的串大小
#include <bits/stdc++.h>
using namespace std;
#define ls st<<1
#define rs st<<1|1
#define fst first
#define snd second
#define MP make_pair
#define PB push_back
#define LL long long
#define PII pair<int,int>
#define VI vector<int>
#define CLR(a,b) memset(a, (b), sizeof(a))
#define ALL(x) x.begin(),x.end()
#define rep(i,s,e) for(int i=(s); i<=(e); i++)
#define tep(i,s,e) for(int i=(s); i>=(e); i--)
const int INF = 0x3f3f3f3f;
const int MAXN = 2e5+10;
const int mod = 1e9+7;
const double eps = 1e-8;
bool flag;
int nxt[MAXN];
int ans;
int n, m;
int arr[MAXN], brr[MAXN];
string s;
void kmp(string p) {
int res = 0;
int i = 0, j = nxt[0] = -1;
int len_p = p.size();
while(i < len_p) {
while(j!=-1 && p[i]!=p[j])
j = nxt[j];
nxt[++i] = ++j;
}
return;
}
int main(int argc, char const *argv[])
{
ios::sync_with_stdio(false);
int T;
cin >> T;
while(T--) {
cin >> s;
kmp(s);
int len = s.size();
if(nxt[len] == 0) {
cout << len <<endl;
continue;
}
int x = len-nxt[len];
if(len%x==0) {
cout << "0\n";
}
else {
cout << x-len%x <<endl;
}
}
return 0;
}