#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <queue>
using namespace std;
#define maxn 110000
int next[maxn],len;
char st[maxn];
void getnext()
{
len=strlen(st);
next[0]=next[1]=0;
for(int i=1;i<len;i++)
{
int j=next[i];
while(j&&st[i]!=st[j])
{
j=next[j];
}
if(st[i]==st[j])
{
next[i+1]=j+1;
}
else
{
next[i+1]=0;
}
}
}
int main()
{
int cas;
scanf("%d",&cas);
while(cas--)
{
scanf("%s",st);
getnext();
int xunhuan=len-next[len];
if(next[len]==0)
{
printf("%d\n",len);
continue;
}
int t=len%(len-next[len]);
if(t==0)
{
printf("0\n");
}
else
printf("%d\n",len-next[len] - next[len] % (len-next[len]));
}
return 0;
}
hdu3746 Cyclic Nacklace KMP判断循环节
最新推荐文章于 2022-06-08 22:01:26 发布
本文介绍了一种基于字符串匹配的算法实现,通过预处理模式串来提高搜索效率。该算法使用next数组记录部分匹配值,能够在主串中快速查找模式串的位置,并处理一些特殊情况,如循环节的识别。

820

被折叠的 条评论
为什么被折叠?



