c语言kmp算法,按照书上写的,但输出结果不对
//试采用KMP算法编写程序,在串s中找到串p第一次出现的位置
# include <stdio.h>
# include <string.h>
#define maxstrlen 20
typedef char sstring[maxstrlen + 1];
void getnext(sstring t,int next[]){
int i = 1;
next[1] = 0;
int j = 0;
while(i<t[0]){
if(j==0 || t[i]==t[j]){
++i;
++j;
next[i]=j;
}else{
j = next[j];
}
}
}
int indexkmp(sstring s,sstring t,int pos,int next[]){
int i = pos;
int j = 1;
while(i<=s[0] && j<=t[0]){
if(j == 0||s[i]==t[j]){
++i;
++j;
}else j = next[j];
}
if(j>t[0]){
return i-t[0];
}else return 0;
}
int main(){
sstring s;
sstring t;//模式串
scanf("%s",s);
scanf("%s",t);
int size=strlen(t);
int next[size];
getnext(t,next);
int pos = 0;
int j = indexkmp(s,t,pos,next);
printf("%d\n",j);
return 0;
}
会输出数但数不对,更新:好像是next里面传参不对,next数组没有改变