Aho–Corasick with One Keyword: the Knuth–Morris–Pratt Special Case #
For a single keyword p, the pattern matching machine of [AhoCorasick1975] is the
Knuth–Morris–Pratt automaton ([KnuthMorrisPratt1977]): the failure state of the prefix of length
q is the prefix of length borderLen p q (the KMP prefix function), and the Aho–Corasick search
reports exactly the KMP match shifts, each shifted to its end position.
Main results #
Cslib.Algorithms.Lean.AhoCorasick.failure_singleton_take:failure [p] (p.take q) = p.take (KMP.borderLen p q)for1 ≤ q ≤ p.length.Cslib.Algorithms.Lean.AhoCorasick.mem_occurrences_singleton_iff:(s + p.length, p) ∈ occurrences [p] t ↔ s ∈ KMP.kmpMatchesList p tforp ≠ [].Cslib.Algorithms.Lean.AhoCorasick.search_singleton:search [p] t = (KMP.kmpMatchesList p t).map fun s => (s + p.length, p)forp ≠ [].
References #
theorem Cslib.Algorithms.Lean.AhoCorasick.failure_singleton_take {α : Type u_1} [DecidableEq α] {p : List α} {q : ℕ} (hq : 0 < q) (hqp : q ≤ p.length) :
For one keyword, the failure function is the KMP prefix function: the failure state of the
prefix of length q is its longest proper border.
theorem Cslib.Algorithms.Lean.AhoCorasick.mem_occurrences_singleton_iff {α : Type u_1} [DecidableEq α] {p t : List α} (hp : p ≠ []) {s : ℕ} :
For one nonempty keyword, its occurrences ending at s + p.length are its KMP matches at
shift s.
theorem Cslib.Algorithms.Lean.AhoCorasick.search_singleton {α : Type u_1} [DecidableEq α] {p : List α} (hp : p ≠ []) (t : List α) :
For one nonempty keyword, the Aho–Corasick search reports exactly the KMP matches, in the same order, each as its end position.