//给出一个字符串,问在其前或者其后最少需要添加几个
//字符才能使其为一个循环字符串
//kmp求出Next数组
//然后循环节的长度为n - Next[n]
#include<cstdio>
#include<cstring>
#include<iostream>
#include<string>
using namespace std ;
const int maxn = 1e5+10 ;
int Next[maxn] ;
string p ;
void get_next()
{
int n = p.length() ;
int j = 0 ;
int k = -1 ;
Next[0] = -1 ;
while(j < n)
{
if((k == -1 || p[k] == p[j]))
{
j++ ;
k++ ;
Next[j] = k ;
}
else k = Next[k] ;
}
}
int main()
{
std::ios_base::sync_with_stdio(false) ;
int t ;
cin >> t ;
while(t--)
{
cin>>p ;
int n = p.length();
get_next() ;
int ans ;
int len = n - Next[n] ;
if(Next[n] && n%len == 0)
ans = 0 ;
else ans = len - Next[n]%len ;
cout<<ans<<endl;
}
return 0 ;
}
03-27
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交