题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3746
kmp模板题以及next数组的应用;
#include<stdio.h>
#include<string.h>
char s[100005];
int next[100005];
void getnext(int n)
{
int i=0,j=-1;
next[0]=-1;
while(i<n)
{
if(j==-1||s[i]==s[j])
next[++i]=++j;
else j=next[j];
}
}
int main(){
int t;
scanf("%d",&t);
while(t--)
{
scanf("%s",s);
int n=strlen(s);
getnext(n);
int sum=n-next[n];
/*n-next[n]代表字符串中的循环节
例如abcab循环节为3,next[n]表示后缀数组最多有多少个与前缀数组相同
详细介绍我会放上我自己学习的链接*/
if(sum!=n&&n%sum==0)
printf("0\n");
else printf("%d\n",sum-next[n]%sum);
}
return 0;
}
个人观摩大牛的链接:
http://blog.csdn.net/fjsd155/article/details/6866991
http://www.cnblogs.com/tangzhengyue/p/4315393.html
新人上路,多多关照;