← capítulo

Aho-Corasick multi-pattern matching

Find thousands of patterns in ONE pass. KMP with a trie. Search cost independent of how many patterns.

Three layers

Watch the automaton scan

Dictionary {he, she, his, hers}, text 'ushers'. ◆ = pattern ends here. 'she' fires she + he (output link); 'r' follows a failure link to 'her'.

One pass vs k passes

Aho-Corasick: flat (one pass, any pattern count). 500 patterns → 260× fewer examinations.

Two things to get right

Takeaway

The tier's theme: precompute STRUCTURE before the scan to make matching cheap. KMP table · rolling hash · skip table · suffix index · this automaton — all the same move. Strings tier done. Next tier: the paradigms behind the algorithms.