Purpose: Maxime Crochemore’s name attaches to several string algorithms. The two that matter are:
- the Two-Way algorithm (Crochemore-Perrin, 1991) — exact matching in time and space;
- Crochemore’s repetitions algorithm (1981) — find all maximal repetitions (squares/runs) in .
Two-Way Algorithm
The practical answer to constant-space linear-time matching, and the algorithm implemented by glibc’s strstr and memmem.
The Idea
Use the critical factorisation theorem: every string has a position splitting it into where the local period at equals the global period of . Such an can be found in time and space by computing the maximal suffix of under both the normal and reversed alphabet orders and taking the better one.
Then match in two directions from that split point:
- Scan left to right. On a mismatch at offset , shift by — no table needed.
- If the right part matches fully, scan right to left. On a mismatch, shift by the period .
- Remember how much of the left part was verified (a single integer of “memory”) so characters are never re-compared.
Complexity
- Preprocessing: time, space
- Search: , at most comparisons
- Space:
This is strictly better than KMP on space and competitive on time, which is why it won the libc slot.
Repetitions Algorithm
Finds all maximal repetitions (runs) — maximal substrings with period — in .
The Idea
Iteratively refine an equivalence-class partition of positions by the substring of length starting there, doubling each round (the same partition-refinement idea as prefix doubling for suffix arrays, and closely related to Hopcroft’s DFA minimisation). Classes with two positions at distance reveal repetitions.
Modern alternative: the runs theorem (Bannai et al., 2015) shows a string of length has fewer than runs, all findable in via Lyndon roots and a suffix structure.
Variants / Use Cases
- Galil-Seiferas — the earlier, more intricate constant-space algorithm Two-Way replaced
- KMP — the contest default; simpler, space
- Main-Lorentz algorithm — divide and conquer for all squares, easier to implement than Crochemore’s
- String fundamentals — periods, borders, critical factorisation
- Data compression — detecting repetitions is the core of LZ-family compressors