← capítulo

Suffix arrays and trees

Index the TEXT once; then find any pattern in O(m log n). A suffix array = the sorted order of all the text's suffixes.

Why sorting suffixes works

Watch binary search on suffixes

Sorted suffixes of 'banana'; search for 'ana'. Orange = midpoint compared · block found = positions [1, 3].

Index amortizes over queries

Build costs 12ms upfront; each query is tiny. Rescan re-reads the text every time. Crossover ≈ 50 queries.

Prefix doubling + LCP

Takeaway

Indexing: pay a fixed build to make repeated queries cheap — worth it past the crossover. Genomics (FM-index), full-text search, BWT compression all live here. Next: Aho-Corasick — one automaton for thousands of patterns.