还是找循环节,和hdu3764一样
直接贴码了
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
char s[1000005];
int next[1000005],flag,n;
void getnext(){
int k=1,j=0;
next[k]= 0;
while(k<=n ){
if( j== 0|| s[k]== s[j] ){
++j, ++k;
next[k]= j;
}
else
j= next[j];
}
}
int main()
{
int i,t=0;
while(scanf("%d",&n) && n!=0)
{
printf("Test case #%d\n",++t);
scanf("%s",&s[1]);
getnext();
for(i=3;i<=n+1;i++){
int flag=i-next[i];
if( (i-1)%flag==0 && (i-1)/flag>1)
printf("%d %d\n",i-1,(i-1)/flag);
}
printf("\n");
}
return 0;
}
本文介绍了一种基于KMP算法的字符串匹配方法,并通过一个具体的C语言程序实现了该算法。该程序能够找出输入字符串中的循环节,适用于需要进行高效字符串匹配的应用场景。
2305

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



