若令next[j]=k,则next[j]表明当模式中第j个字符与主串中相应字符“失配”时,在模式中需重新和主串中该字符进行比较的字符的位置。由此可引出模式串的next函数的定义:
1.next[j]=-1 当j=0时
2.next[j]=Max{k|0
此集合不为空时
3.next[j]=1 其他情况
其中p0p1…pk-1为p0p1p2…pj-2pj-1的前缀子串,pj-kpj-k+1…pj-1为p0p1p2…pj-2pj-1的后缀子串。
KMP算法源代码如下:
#include
#include
#define MAXSIZE 100
void get_nextval(unsigned char pat[],int nextval[])
{
int length = strlen(pat);
int i=1;
int j=0;
nextval[1]=0;
while(i {
if(j==0||pat[i-1]==pat[j-1])
{
++i;
++j;
if(pat[i-1]!=pat[j-1]) nextval[i]=j;
else nextval[i]=nextval[j];
}
else j=nextval[j];
}
}
int Index_KMP(unsigned char text[], unsigned char pat[],int nextval[])
{
int i=1;
int j=1;
int t_len = strlen(text);
int p_len = strlen(pat);
while (i<=t_len&&j<=p_len)
{
if(j==0||text[i-1]==pat[j-1]){++i;++j;}
else j=nextval[j];
}
if (j>p_len) return i-1-p_len;
else return -1;
}
int main()
{
unsigned char text[MAXSIZE];
unsigned char pat[MAXSIZE];
int nextval[MAXSIZE];
int answer, i;
printf("\nBoyer-Moore String Searching Program");
printf("\n====================================");
printf("\n\nText String --> ");
gets(text);
printf( "\nPattern String --> ");
gets(pat);
get_nextval(pat,nextval);
if((answer=Index_KMP(text, pat,nextval))>=0)
{
printf("\n");
printf("%s\n", text);
for (i = 0; i < answer; i++)
printf(" ");
printf("%s", pat);
printf("\n\nPattern Found at location %d\n", answer);
}
else
printf("\nPattern NOT FOUND.\n");
return 0;
}