(1) 引子:
对于2个字符串A,B,其中A为主串,B为待匹配的子串,如
i 1 2 3 4 5 6 7 8 ...
A:a b a a c c a b a b a a b c a c b a c a c ...
B:a b a a b c a c
j 1 2 3 4 5 6 7 8 ...
当A[5] != B[5]时,蛮力法是从头再来,而KMP算法则不同,它采用一定的策略,调整j的位置,使得B[j] == A[i]。
(2) KMP主代码:
<textarea cols="50" rows="15" name="code" class="cpp:nogutter">int KMPSearch(const char * main_str, const char * sub_str, int * next) { int i=0; int j=0; int main_len=strlen(main_str); int sub_len=strlen(sub_str); while((i<main_len)&&(j<sub_len)) { if(j == -1 || main_str[i] == sub_str[j]) { j++; i++; }else{ j=next[j]; } } if(j == sub_len){ return i-sub_len; }else{ return -1; } }</textarea>
(3)从(2)中的代码可以看出,KMP算法的关键是构造next表。
1 next[i]表示的是:
在第i个字符前面的i-1个字符里面,
从开头开始的1个字符与最后1个字符是否相等,若不是,则next[i]=0,否则继续看下面;
从开头开始的2个字符与最后2个字符是否相等,若不是,则next[i]=1,否则继续看下面;
从开头开始的3个字符与最后3个字符是否相等,若不是,则next[i]=2,否则继续看下面;
……
2 next数组求解思路:
第1位的next值为-1,第2位的next值为0,后面求解每一位的next值时,根
据前一位进行比较。
比较策略如下:
(1) 将前一位与其(next值对应的字符)进行比较;
(2) 如果相等,则该位的next值就是前一位的next值加上1;
(3) 如果不等,向前继续寻找(next值对应的字符)来与前一位进行比较;
(4) 如果最后找到,则在这个位对应的值加上1,即为所求的next值;
(5) 如果最后没有找到,那么所求next值为0。
3 next函数如下:
<textarea cols="50" rows="15" name="code" class="cpp:nogutter">void CalculateNext(const char * sub_str, int len, char * next) { int i=-1; int j=0; next[0]=-1; while(j<len){ //i == -1表示找到最后,策略(5) //sub_str[i] == sub_str[j]表示找到,策略(1)(2) if(i == -1 || sub_str[i] == sub_str[j]){ //策略(2)(4) i++; j++; next[j]=i; }else{ //策略(3) i=next[i]; } } }</textarea>
附:
<textarea cols="50" rows="15" name="code" class="cpp">void CalculateNext(const char * sub_str, int len, int * next) { int i=-1; int j=0; next[0]=-1; while(j<len){ if(i == -1 || sub_str[i] == sub_str[j]){ i++; j++; next[j]=i; }else{ i=next[i]; } } } int KMPSearch(const char * main_str, const char * sub_str) { int i=0; int j=0; int * next; int main_len=strlen(main_str); int sub_len=strlen(sub_str); next = (int*)malloc(sizeof(int)*sub_len + 1); CalculateNext(sub_str, sub_len, next); while((i<main_len)&&(j<sub_len)) { if(j == -1 || main_str[i] == sub_str[j]) { j++; i++; }else{ j=next[j]; } } if(j == sub_len){ return i-sub_len; }else{ return -1; } } </textarea>