📜  kmp 算法 - 任何代码示例

📅  最后修改于: 2022-03-11 14:55:57.455000             🧑  作者: Mango

代码示例3
KMP-MATCHER (T, P)
 1. n ← length [T]
 2. m ← length [P]
 3. Π← COMPUTE-PREFIX-FUNCTION (P)
 4. q ← 0        // numbers of characters matched
 5. for i ← 1 to n    // scan S 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