data-structurestring
what
- An advanced pattern searching algorithm for finding all occurrences of a set of strings p1,p2,…,pk inside a target string t.
- Based on a trie with added links:
- suffix links: point to the node represents the longest suffix of the current query.
- output links: point to the node represents the longest suffix of the current query which is also a terminal node.
complexity
For
- m=∣t∣ is the length of the target string
- n=∣p1∣+∣p2∣+…+∣pk∣ is the total length of the patterns
- z is the total number of matches
Time: O(n+m+z)
- O(n) for constructing the trie
- O(n) for constructing the suffix links and output links
- O(m+z) for finding all the matches
- or O(zi⋅m) with zi is the number of matches at each position of t
usage
- Great for cases where the patterns are fixed and the target text changes.
implementation
references