String Searching
Authors: Benjamin Qi, Siyong Huang
Prerequisites
Knuth-Morris-Pratt and Z Algorithms (and a few more related topics).
| Resources | |||
|---|---|---|---|
| CPC | String Matching, KMP, Tries | ||
| CP2 | |||
Single String
KMP
Knuth-Morris-Pratt, or KMP, is a linear time string comparison algorithm that matches prefixes. Specifically, it computes the longest substring that is both a prefix and suffix of a string, and it does so for every prefix of a given string.
| Resources | |||
|---|---|---|---|
| cp-algo | |||
| PAPS | |||
| GFG | |||
| TC | |||
| Status | Source | Problem Name | Difficulty | Tags | |||||
|---|---|---|---|---|---|---|---|---|---|
| Kattis | Easy | Show TagsKMP, Strings | |||||||
| POI | Easy | Show TagsKMP, Strings | |||||||
| Baltic OI | Normal | Show TagsKMP, Strings | |||||||
| POJ | Hard | Show TagsKMP, Strings | |||||||
| POI | Hard | Show TagsKMP, Strings | |||||||
| CEOI | Hard | Show TagsKMP | |||||||
| POI | Very Hard | Show TagsKMP | |||||||
| POI | Very Hard | Show TagsKMP | |||||||
Z Algorithm
The Z-Algorithm is another linear time string comparison algorithm like KMP, but instead finds the longest common prefix of a string and all of its suffixes.
| Resources | |||
|---|---|---|---|
| cp-algo | |||
| CPH | |||
| CF | |||
| Status | Source | Problem Name | Difficulty | Tags | |||||
|---|---|---|---|---|---|---|---|---|---|
| YS | Easy | ||||||||
| CF | Normal | Show TagsDP, Strings | |||||||
| CF | Hard | ||||||||
Palindromes
Manacher
Focus Problem – read through this problem before continuing!
Manacher's Algorithm functions similarly to the Z-Algorithm. It determines the longest palindrome centered at each character.
| Resources | |||
|---|---|---|---|
| HR | |||
| CF | shorter code | ||
| cp-algo | |||
Don't Forget!
If s[l, r] is a palindrome, then s[l+1, r-1] is as well.
| Status | Source | Problem Name | Difficulty | Tags | |||||
|---|---|---|---|---|---|---|---|---|---|
| CF | Normal | Show TagsStrings | |||||||
| CF | Normal | Show TagsStrings | |||||||
| CF | Hard | Show TagsPrefix Sums, Strings | |||||||
Palindromic Tree
A Palindromic Tree is a tree-like data structure that behaves similarly to KMP. Unlike KMP, in which the only empty state is , the Palindromic Tree has two empty states: length , and length . This is because appending a character to a palindrome increases the length by , meaning a single character palindrome must have been created from a palindrome of length
| Resources | |||
|---|---|---|---|
| CF | |||
| adilet.org | |||
| Status | Source | Problem Name | Difficulty | Tags | |||||
|---|---|---|---|---|---|---|---|---|---|
| APIO | Easy | ||||||||
| CF | Hard | Show TagsPrefix Sums, Strings | |||||||
| DMOJ | Very Hard | ||||||||
Multiple Strings
Tries
A trie is a tree-like data structure that stores strings. Each node is a string, and each edge is a character. The root is the empty string, and every node is represented by the characters along the path from the root to that node. This means that every prefix of a string is an ancestor of that string's node.
| Resources | |||
|---|---|---|---|
| CPH | |||
| CF | |||
| PAPS | |||
| Status | Source | Problem Name | Difficulty | Tags | |||||
|---|---|---|---|---|---|---|---|---|---|
| COCI | Very Easy | Show TagsDFS, Strings, Trie | |||||||
| IOI | Very Easy | Show TagsDFS, Strings, Trie | |||||||
| YS | Easy | Show TagsGreedy, Trie | |||||||
| CF | Normal | Show TagsStrings, Trie | |||||||
| COCI | Normal | Show TagsTrie | |||||||
| IZhO | Hard | Show TagsGreedy, Trie | |||||||
| JOI | Hard | Show TagsBIT, Trie | |||||||
| CF | Hard | Show TagsTree, Trie | |||||||
Aho-Corasick
Aho-Corasick is the combination of trie and KMP. It is essentially a trie with KMP's "fail" array.
Warning!
Build the entire trie first, and then run a BFS to construct the fail array.
| Resources | |||
|---|---|---|---|
| cp-algo | |||
| CF | |||
| GFG | |||
| Status | Source | Problem Name | Difficulty | Tags | |||||
|---|---|---|---|---|---|---|---|---|---|
| CF | Easy | Show TagsStrings | |||||||
| Gold | Normal | Show TagsStrings | |||||||
| CF | Normal | Show TagsStrings | |||||||
This section is not complete.
1731 Word Combinations -> trie 1753 String Matching -> string search 1732 Finding Borders -> string search 1733 Finding Periods -> string search 1110 Minimal Rotation -> string search 1111 Longest Palindrome -> string search 1112 Required Substring -> string search
Module Progress:
Join the USACO Forum!
Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!