题目
这个就是求,若一个字符串有不完整的循环节,求要补足多少个字符才能变完整。
记一下公式吧
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<vector>
#include<cmath>
#include<map>
#include<string>
#include<queue>
#include<stack>
#include<bitset>
#include<list>
#define IO ios::sync_with_stdio(false)
//#define int long long
using namespace std;
char s[100000+5];
int len,t,kmp[100000+5];
void getk()
{memset(kmp,0,sizeof(kmp));int j=0;for(int i=2;i<=len;i++){while(j&&s[i]!=s[j+1]){j=kmp[j];}if(s[j+1]==s[i])j++;kmp[i]=j;}
}
int main()
{ios::sync_with_stdio(false);cin>>t;while(t--){cin>>s+1;len=strlen(s+1);getk();if(kmp[len]==0){printf("%d\n",len);}else if(len%(len-kmp[len])==0){printf("0\n");}else{int len1=len-kmp[len];printf("%d\n",len1-len%len1);}}
}