Technique
Aho-Corasick Automaton
Build a trie of patterns plus KMP-style failure links so one pass over the text finds all occurrences of all patterns.
0%
mastery
0/5 solved
0/5 solved
Recognise it
The signal
many patterns, one text
1Brute force
Run KMP once per pattern.
2The observation
Per-pattern scans: failure links share partial matches across patterns.
3Why it works
At each text position, the current state is the longest pattern-trie prefix that is a suffix of the text read.
Reach for it when
- +count occurrences of each dictionary word
- +DP over text avoiding forbidden words
Not when
- ×single pattern (KMP)
- ×substring queries on the text (suffix-array)
Practice
0/5 solved
- Finding PatternsCSESMedium
- Counting PatternsCSESHard
- Pattern PositionsCSESHard
- Strings Containing Exactly B Given WordsMedia.netOAHard
- You Are Given Some Strings...Codeforces2400
Pitfalls
Misconceptions
- !forgetting to propagate counts along output/failure links