您的位置:首页 > 其它

hdu 3746 Cyclic Nacklace(kmp)

2016-05-14 11:14 309 查看
传送门

#include <stdio.h>
#include <string.h>
char s[100010];
int next[100010];
int main()
{
int T,i,j,len,length;
scanf("%d",&T);
while(T--)
{
scanf("%s",s);
len = strlen(s);
i = 0;
j = -1;
next[0] = -1;
while(i < len)//获得next数组
{
if(j == -1 || s[i] == s[j])
{
++i;
++j;
next[i] = j;
}
else
j = next[j];
}
length = len - next[len];//循环节长度
if(len != length && len%length == 0)//如果字符串是循环的
printf("0\n");
else
printf("%d\n",length - next[len]%length);//取模作用,如:abcab,去掉abc
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: