#include <iostream>
#include <cstdio>
#include <cstring>
#define MAXN 10+1000
using namespace std;
//P[0]不使用,P[i]表示B串的前i個字符中, 前P[i]個字符和後P[i]個字符相同
int Next[MAXN];
char* KMP(char* A, char* B); //返回B串在A串中的位置 B串:模式串 A串:待匹配串
void build_next(char *B); //將B串(模式串)進行自我匹配
char a[]="ababababbc";
char b[]="ababbc";
int main()
{
build_next(b);
char *idx = KMP(a, b);
if(!idx)
puts("b is not a's substring.");
else
printf("b is a's substring, the index is %d\n",idx-a);
return 0;
}
char* KMP(char *A, char *B)
{
int len1=strlen(A), len2=strlen(B);
int i, j=0; //j代表目前B串中已與A串匹配了的字符的個數
for(i=0; i<len1; ++i)
{
while(j>0 && A[i]!=B[j]) //0...j-1,已匹配了j個字符,sub[j]是sub的第j+1的字符,因爲下標從0開始
j = Next[j];
if(A[i] == B[j])
++j;
if(j == len2) //當j(B中已匹配了的字符串的個數)與B串本身長度相等時,說明匹配完畢
return A+i-j+1; //此時可計算出指針位置
}
return NULL;
}
void build_next(char *B)
{
Next[0] = 0;
Next[1] = 0;
int i, j=0, len=strlen(B);
for(i=2; i<len; ++i)
{
/*while(j>0 && B[j]!=B[i-1]), 這裏的j代表
在比較第i個字符(從1開始數)和B[j]時,前i-1個字符中的前j個字符和後j的字符相同
B[j]代表第j的字符(從1開始數)的下一個字符*/
while(j>0 && B[j]!=B[i-1]) //如果第i個字符和第j個字符的下一個字符(即B[j])不同,則改變j的值,再重新比較
j = Next[j];
if(B[j] == B[i-1])
++j;
Next[i] = j;
}
}
下面是主要代碼部分:
char* KMP(char *A, char *B)
{
int len1=strlen(A), len2=strlen(B);
int i, j=0; //j代表目前B串中已與A串匹配了的字符的個數
for(i=0; i<len1; ++i)
{
while(j>0 && A[i]!=B[j]) //0...j-1,已匹配了j個字符,sub[j]是sub的第j+1的字符,因爲下標從0開始
j = Next[j];
if(A[i] == B[j])
++j;
if(j == len2) //當j(B中已匹配了的字符串的個數)與B串本身長度相等時,說明匹配完畢
return A+i-j+1; //此時可計算出指針位置
}
return NULL;
}
void build_next(char *B)
{
Next[0] = 0;
Next[1] = 0;
int i, j=0, len=strlen(B);
for(i=2; i<len; ++i)
{
/*while(j>0 && B[j]!=B[i-1]), 這裏的j代表
在比較第i個字符(從1開始數)和B[j]時,前i-1個字符中的前j個字符和後j的字符相同
B[j]代表第j的字符(從1開始數)的下一個字符*/
while(j>0 && B[j]!=B[i-1]) //如果第i個字符和第j個字符的下一個字符(即B[j])不同,則改變j的值,再重新比較
j = Next[j];
if(B[j] == B[i-1])
++j;
Next[i] = j;
}
}