假設你有一個字符串Hello World!!!
,你要搜索Head Up
。
Hello World!!!
Head Up
^
當你在第一和第二個字符的首要條件適用(first case: the substring continues)
,在顯着位置的情況下,字符不匹配,但你已經是一個子字符串匹配(2字內匹配到那裏),這種情況對應於第二個條件(second case: it doesn't, but we can fall back)
。第三種情況是當你錯過匹配模式的第一個字符時。
第二個條件是必要的,因爲您可以使用匹配的字符的信息,直到錯過匹配,以避免不必要的比較,您可能已經知道結果(跳過string
的字符,你已經知道,開始部分該模式將不匹配)。
示例:對於串HeHello World!!!
並搜索Hello
HeHello World!!!
Hello
^when you miss match this character using the table of KMP you known that
could skip 2 characters because
HeHello World!!!
Hello
^ this would miss match
在建設模式表爲圖案HeHello
的情況下。假設^
是cnd
和*
是pos
。起始點是pos = 2
和cnd = 0
(但是當檢查模式是pos - 1 = 1
時)。
HeHeHello T [-1,0,0,0,0,0,0,0,0]
^* comparing 0 with 1 go to condition 3 cnd = 0, pos = 2
_
HeHeHello T [-1,0,0,1,0,0,0,0,0]
^ * comparing 0 with 2 go to condition 1 cnd = 0, pos = 3
_
HeHeHello T [-1,0,0,1,2,0,0,0,0]
^ * comparing 1 with 3 go to condition 1 cnd = 1, pos = 4
_
HeHeHello T [-1,0,0,1,2,3,0,0,0]
^* comparing 2 with 4 go to condition 1 cnd = 2, pos = 5
_
HeHeHello T [-1,0,0,1,2,3,4,0,0]
^* comparing 3 with 5 go to condition 1 cnd = 3, pos = 6
HeHeHello T [-1,0,0,1,2,3,4,0,0]
^* comparing 4 with 6 go to condition 2 (cnd = T[cnd], cnd = T[4] = 2)
HeHeHello T [-1,0,0,1,2,3,4,0,0]
^ * comparing 2 with 6 go to condition 2 (cnd = T[cnd], cnd = T[2] = 0)
...
嗨!你提供的解釋是爲了當我想將一個模式'Hello'與字符串'Hello world'匹配時。但我的問題是關於表格構建算法。在表格構建算法中,對於字符串「Hello」,第一種情況是錯誤的。 – 2014-09-10 16:58:57
更新了一個例子 – NetVipeC 2014-09-10 18:15:30