之前看过KMP的算法,道理挺简单,但是算法实现有点不好懂。近来重温,转一篇写的比较好的blog。
[url]http://www.cppblog.com/oosky/archive/2006/07/06/9486.html[/url]
这个求模式值的函数逆天了,反正我写不出来 :wink:
[url]http://www.cppblog.com/oosky/archive/2006/07/06/9486.html[/url]
这个求模式值的函数逆天了,反正我写不出来 :wink:
void nextIndex(const char *t, int next[]) {
int j=0,k=-1;
next[0] = -1;
while(t[j] != '\0') {
if(k == -1 || t[k] == t[j]) {
++j,++k;
if(t[k] != t[j])
next[j] = k;
else
next[j] = next[k];
}
else
k = next[k];
}
}