对kmp算法的理解可以参考Introduction to Algorithm 3rd Edition Chapter 32.4
如果没有这本书的同学,可以看看下面
KMP-MATCHER(T, P)
1 n ← length[T]
2 m ← length[P]
3 π ← COMPUTE-PREFIX-FUNCTION(P)
4 q ← 0 ▹Number of characters matched.
5 for i ← 1 to n ▹Scan the text from left to right.
6 do while q > 0 and P[q + 1] ≠ T[i]
7 do q ← π[q] ▹Next character does not match.
8 if P[q + 1] = T[i]
9 then q ← q + 1 ▹Next character matches.
10 if q = m ▹Is all of P matched?
11 then print "Pattern occurs with shift" i - m
12 q ← π[q] ▹Look for the next match.
COMPUTE-PREFIX-FUNCTION(P)
1 m ← length[P]
2 π[1] ← 0
3 k ← 0
4 for q ← 2 to m
5 do while k > 0 and P[k + 1] ≠ P[q]
6 do k ← π[k]
7 if P[k + 1] = P[q]
8 then k ← k + 1
9 π[q] ← k
10 return π
很多网页上面都有对该算法的解释和说明
<1> http://blog.csdn.net/liuben/archive/2009/08/04/4409505.aspx
<2> http://www.ics.uci.edu/~eppstein/161/960227.html
<3> http://en.wikipedia.org/wiki/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm
<4> http://www.inf.fh-flensburg.de/lang/algorithmen/pattern/kmpen.htm
ps2: 下面是用c语言按照网页索引<3>的一个小实现
http://en.wikipedia.org/wiki/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
void kmp_table(const char* w, int **t)
{
int pos = 2;
int cnd = 0;
assert(NULL != w && NULL != t);
*t = (int *)malloc(strlen(w) + 1);
if(NULL != *t)
memset(*t, 0x00, strlen(w) + 1);
else
exit(EXIT_FAILURE);
**t = -1;
*(*t + 1) = 0;
while( pos < strlen(w)) {
if(*(w + pos - 1) == *(w + cnd)){
*(*t + pos) = cnd + 1;
pos++;
cnd++;
}
else if(cnd > 0) {
cnd = *(*t + cnd);
}
else {
*(*t + pos) = 0;
pos++;
}
}
#ifdef DEBUG
{
int i = -1;
while(++i < strlen(w)) {
printf("table[%d] is %d/n", i, *(*t + i));
}
}
#endif
}
void kmp_freetable(int *t)
{
if(NULL != t )
free((void*)t);
}
int kmp_search(const char *s, const char* w)
{
int m = 0, i = 0;
int *table;
kmp_table(w, &table);
while((m + i) < strlen(s)) {
if(*(w + i) == *(s + m + i)) {
i++;
if(i == strlen(w)) {
return m;
}
}
else {
m = m + i - table[i];
if(i > 0)
i = table[i];
}
}
kmp_freetable(table);
/* since we got here, no match found */
return strlen(s);
}
int main ( int argc, char *argv[] )
{
char* s = "abc abcdab abcdabcdabde";
char* t = "abcdabd";
printf("result %d/n", kmp_search(s, t));
return EXIT_SUCCESS;
}