Input
The first line of the input is a single integer T ( 0 < T <= 100 ) which means the number of test cases.
Each test case contains only one line describe the original ordinary chain to be remade. Each character in the string stands for one pearl and there are 26 kinds of pearls being described by ‘a’ ~‘z’ characters. The length of the string Len: ( 3 <= Len <= 100000 ).
Output
For each case, you are required to output the minimum count of pearls added to make a CharmBracelet.
Sample Input
3
aaa
abca
abcde
Sample Output
0
2
5
题解链接:https://blog.csdn.net/reverie_mjp/article/details/53088612
#include <stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int N = 1e6;
int Next[N + 5];
char s1[N + 5], s2[N + 5];
char temp[N + 5];
int len1, len2;
int m;
void get_next()
{
int i, j;
i = 0;
Next[0] = j = -1;
while(i < len1) {
if(j == -1 || s1[i] == s1[j]) Next[++i] = ++j;
else j = Next[j];
}
}
void kmp()
{
int i, j;
i = j = 0;
while(i < len1) {
if(j == -1 || s1[i] == s2[j]) ++i, ++j;
else j = Next[j];
}
}
int main()
{
int t,n;
scanf("%d",&t);
while(t--)
{
scanf("%s",s1);
len1 = strlen(s1);
get_next();
int cyc=len1-Next[len1]; //最小周期
if(len1%cyc==0&&len1/cyc!=1)//如果字符串内包含一个以上的周期
printf("0\n");
else
printf("%d\n",cyc-len1%cyc);//不包含周期的情况或者仅有一个周期的情况
}
return 0;
}