Expand description
Suffix Automaton (SAM).
A suffix automaton is a directed acyclic automaton that recognises
exactly the set of substrings of a given string. It has at most
2n - 1 states for an input of length n, and every state
corresponds to a distinct set of end positions in the original
string — hence to a distinct set of substrings.
Once built, you can answer:
- Is
pa substring ofs? (O(|p|)) - How many distinct substrings does
shave? (sum over states oflen(state) - len(link(state))) - Number of occurrences of a pattern (when
state.occis computed) - Longest common substring between two strings
§Example
use suffix_automaton::SuffixAutomaton;
let mut sam = SuffixAutomaton::new();
for ch in "ababa".chars() {
sam.extend(ch);
}
assert!(sam.contains("aba"));
assert!(sam.contains("bab"));
assert!(!sam.contains("bb"));Structs§
- State
- State of a suffix automaton.
- Suffix
Automaton - Suffix Automaton over a sequence of
chars.