KMP算法代码实现
#include <stdio.h>#include <string.h>
int index_KMP(char *s,char *t,int pos);
void get_next(char *t,int *);
char s="abcacbcba";
char t="bca";
int next;
int pos=0;
int main()
{
int n;
get_next(t,next);
n=index_KMP(s,t,pos);
printf("%d",n);
return 0;
}
int index_KMP(char *s,char *t,int pos)
{
int i=pos,j=1;
while (i<=(int)strlen(s)&&j<=(int)strlen(t))
{
if (j==0||s==t)
{
i++;
j++;
}
else
j=next;
}
if (j>(int)strlen(t))
return i-strlen(t)+1;
else
return 0;
}
void get_next(char *t,int *next)
{
int i=1,j=0;
next=next=0;
while (i<(int)strlen(t))
{
if (j==0||t==t)
{
i++;
j++;
next=j;
}
else
j=next;
}
}
自己顶一个了 自己再来一个了 我来顶一个 来看看支持下。
页:
[1]