ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
StringMatching.h

The exact string matching, beyond the book. More...

Files

file  ADS/string/StringMatching.h
 The exact string matching, beyond the book: the occurrences of a pattern \( P \) of \( m \) characters in a text \( T \) of \( n \) characters, i.e., the places \( s \in [ 0 , n - m ] \) with \( T[ s .. s + m - 1 ] = P \).

Functions

std::vector< std::size_t > ADS::naiveMatching (std::string_view T, std::string_view P)
 the places of all the occurrences of \( P \) in \( T \), by trying every shift
std::vector< long > ADS::failure (std::string_view P)
 the failure function: \( F[ q ] \) is the length of the longest proper border of \( P[ 0 .. q - 1 ] \), and \( F[ 0 ] = -1 \)
std::vector< std::size_t > ADS::kmp (std::string_view T, std::string_view P, long *comparisons=nullptr)
 the places of all the occurrences of \( P \) in \( T \), by Knuth-Morris-Pratt; the comparisons of characters go to *comparisons, if given
std::vector< long > ADS::strongFailure (std::string_view P)
 the strong failure function: \( G[ q ] = F[ q ] \) unless \( P[ F[ q ] ] = P[ q ] \), in which case \( G[ q ] = G[ F[ q ] ] \); \( G[ m ] = F[ m ] \)
std::vector< std::size_t > ADS::kmpStrong (std::string_view T, std::string_view P, long *comparisons=nullptr)
 the occurrences of kmp() above, with the strong failure function on a mismatch
std::vector< std::size_t > ADS::boyerMoore (std::string_view T, std::string_view P, long *comparisons=nullptr)
 the places of all the occurrences of \( P \) in \( T \), by Boyer-Moore with the bad character and the strong good suffix rules
std::vector< std::size_t > ADS::horspool (std::string_view T, std::string_view P, long *comparisons=nullptr)
 the places of all the occurrences of \( P \) in \( T \), by Horspool
std::vector< std::size_t > ADS::shiftOr (std::string_view T, std::string_view P)
 the places of all the occurrences of \( P \) in \( T \), by Shift-Or
std::vector< std::size_t > ADS::zFunction (std::string_view S)
 the Z-function: \( Z[ i ] \) is the length of the longest common prefix of \( S \) and \( S[ i .. ] \), and \( Z[ 0 ] = |S| \)
std::vector< std::size_t > ADS::zMatching (std::string_view T, std::string_view P)
 the places of all the occurrences of \( P \) in \( T \), by the Z-function of \( P \) and the Z-boxes of \( T \)
std::vector< std::size_t > ADS::twoWay (std::string_view T, std::string_view P)
 the places of all the occurrences of \( P \) in \( T \), by the Two-Way algorithm of Crochemore and Perrin

Detailed description

The exact string matching, beyond the book.

The definitions, the invariants and the costs of the area are in Theory of strings.

Function documentation

◆ boyerMoore()

std::vector< std::size_t > ADS::boyerMoore ( std::string_view T,
std::string_view P,
long * comparisons = nullptr )
inline

the places of all the occurrences of \( P \) in \( T \), by Boyer-Moore with the bad character and the strong good suffix rules

In pseudocode (Boyer and Moore, beyond the book), \( \mathit{bc} \) the table of horspool() and \( \mathit{gs}[ i ] \) the shift of the good suffix after a mismatch at \( i \), both computed from \( P \) alone; each rule shifts by no more than the next place where the window can match:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{boyerMoore}( T , P ) \cr 2 & \quad \mathit{bc} \gets \text{the bad characters of } P; \ \mathit{gs} \gets \text{the good suffixes of } P \cr 3 & \quad j \gets 0;\ R \gets \emptyset \cr 4 & \quad \textbf{while}\ j \le n - m\ \textbf{do} \cr 5 & \qquad i \gets m - 1 \cr 6 & \qquad \textbf{while}\ i \ge 0\ \textbf{and}\ P[ i ] = T[ j + i ] \ \textbf{do}\ i \gets i - 1 \cr 7 & \qquad \textbf{if}\ i < 0\ \textbf{then}\ R \gets R \cup \{ j \}; \ j \gets j + \mathit{gs}[ 0 ] \cr 8 & \qquad \textbf{else}\ j \gets j + \max\{ \mathit{gs}[ i ] , \mathit{bc}[ T[ j + i ] ] - ( m - 1 - i ) \} \cr 9 & \quad \textbf{return}\ R \end{array}} \]

Postcondition
the occurrences of naiveMatching( T , P ) (audit)

◆ failure()

std::vector< long > ADS::failure ( std::string_view P)
inline

the failure function: \( F[ q ] \) is the length of the longest proper border of \( P[ 0 .. q - 1 ] \), and \( F[ 0 ] = -1 \)

In pseudocode (Knuth, Morris and Pratt, beyond the book), \( k \) the border of the prefix read so far, shortened by the failures until it can be extended:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{failure}( P ) \cr 2 & \quad F[ 0 ] \gets -1;\ k \gets -1 \cr 3 & \quad \textbf{for}\ q \gets 0\ \textbf{to}\ m - 1\ \textbf{do} \cr 4 & \qquad \textbf{while}\ k \ge 0\ \textbf{and}\ P[ k ] \ne P[ q ] \ \textbf{do}\ k \gets F[ k ] \cr 5 & \qquad k \gets k + 1;\ F[ q + 1 ] \gets k \cr 6 & \quad \textbf{return}\ F \end{array}} \]

Postcondition
\( F[ q ] < q \) for \( q > 0 \)

◆ horspool()

std::vector< std::size_t > ADS::horspool ( std::string_view T,
std::string_view P,
long * comparisons = nullptr )
inline

the places of all the occurrences of \( P \) in \( T \), by Horspool

In pseudocode (Horspool's algorithm, beyond the book), the window compared from its last character and shifted by the bad character of that character:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{horspool}( T , P ) \cr 2 & \quad \textbf{foreach}\ c \in \Sigma\ \textbf{do}\ \mathit{bc}[ c ] \gets m \cr 3 & \quad \textbf{for}\ i \gets 0\ \textbf{to}\ m - 2\ \textbf{do} \ \mathit{bc}[ P[ i ] ] \gets m - 1 - i \cr 4 & \quad j \gets 0;\ R \gets \emptyset \cr 5 & \quad \textbf{while}\ j \le n - m\ \textbf{do} \cr 6 & \qquad \textbf{if}\ T[ j .. j + m - 1 ] = P \text{ (from the right)} \ \textbf{then}\ R \gets R \cup \{ j \} \cr 7 & \qquad j \gets j + \mathit{bc}[ T[ j + m - 1 ] ] \cr 8 & \quad \textbf{return}\ R \end{array}} \]

Postcondition
the occurrences of naiveMatching( T , P ) (audit)

◆ kmp()

std::vector< std::size_t > ADS::kmp ( std::string_view T,
std::string_view P,
long * comparisons = nullptr )
inline

the places of all the occurrences of \( P \) in \( T \), by Knuth-Morris-Pratt; the comparisons of characters go to *comparisons, if given

In pseudocode (Knuth, Morris and Pratt, beyond the book), \( q \) the characters of \( P \) matched, which never decrease below the border of what was matched; \( i \) never goes back, which gives at most \( 2 n \) comparisons:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{kmp}( T , P ) \cr 2 & \quad F \gets \mathrm{failure}( P );\ q \gets 0;\ R \gets \emptyset \cr 3 & \quad \textbf{for}\ i \gets 0\ \textbf{to}\ n - 1\ \textbf{do} \cr 4 & \qquad \textbf{while}\ q \ge 0\ \textbf{and}\ P[ q ] \ne T[ i ] \ \textbf{do}\ q \gets F[ q ] \cr 5 & \qquad q \gets q + 1 \cr 6 & \qquad \textbf{if}\ q = m\ \textbf{then}\ R \gets R \cup \{ i + 1 - m \};\ q \gets F[ m ] \cr 7 & \quad \textbf{return}\ R \end{array}} \]

Postcondition
the occurrences of naiveMatching( T , P )

◆ kmpStrong()

std::vector< std::size_t > ADS::kmpStrong ( std::string_view T,
std::string_view P,
long * comparisons = nullptr )
inline

the occurrences of kmp() above, with the strong failure function on a mismatch

In pseudocode (Knuth, Morris and Pratt, beyond the book):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{kmpStrong}( T , P ) \cr 2 & \quad \textbf{return}\ \mathrm{kmp}( T , P ) \text{ with } \mathrm{strongFailure}( P ) \text{ in place of } F \text{ in its } \text{line 4} \end{array}} \]

Postcondition
the occurrences of naiveMatching( T , P )

◆ naiveMatching()

std::vector< std::size_t > ADS::naiveMatching ( std::string_view T,
std::string_view P )
inline

the places of all the occurrences of \( P \) in \( T \), by trying every shift

In pseudocode (the naive algorithm, beyond the book):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{naiveMatching}( T , P ) \cr 2 & \quad R \gets \emptyset \cr 3 & \quad \textbf{for}\ s \gets 0\ \textbf{to}\ n - m\ \textbf{do} \cr 4 & \qquad \textbf{if}\ T[ s .. s + m - 1 ] = P\ \textbf{then}\ R \gets R \cup \{ s \} \cr 5 & \quad \textbf{return}\ R \end{array}} \]

◆ shiftOr()

std::vector< std::size_t > ADS::shiftOr ( std::string_view T,
std::string_view P )
inline

the places of all the occurrences of \( P \) in \( T \), by Shift-Or

In pseudocode (Baeza-Yates and Gonnet, beyond the book), the bit \( i \) of \( D \) clear if and only if \( P[ 0 .. i ] \) ends at \( T[ j ] \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{shiftOr}( T , P ) \cr 2 & \quad \textbf{foreach}\ c \in \Sigma\ \textbf{do}\ B[ c ] \gets 1^{64} \cr 3 & \quad \textbf{for}\ i \gets 0\ \textbf{to}\ m - 1\ \textbf{do} \ \text{clear the bit } i \text{ of } B[ P[ i ] ] \cr 4 & \quad D \gets 1^{64};\ R \gets \emptyset \cr 5 & \quad \textbf{for}\ j \gets 0\ \textbf{to}\ n - 1\ \textbf{do} \cr 6 & \qquad D \gets ( D \ll 1 ) \vee B[ T[ j ] ] \cr 7 & \qquad \textbf{if}\ \text{the bit } m - 1 \text{ of } D \text{ is } 0\ \textbf{then}\ R \gets R \cup \{ j + 1 - m \} \cr 8 & \quad \textbf{return}\ R \end{array}} \]

Precondition
\( m \le 64 \)
Postcondition
the occurrences of naiveMatching( T , P ) (audit)

◆ strongFailure()

std::vector< long > ADS::strongFailure ( std::string_view P)
inline

the strong failure function: \( G[ q ] = F[ q ] \) unless \( P[ F[ q ] ] = P[ q ] \), in which case \( G[ q ] = G[ F[ q ] ] \); \( G[ m ] = F[ m ] \)

In pseudocode (Knuth, Morris and Pratt, beyond the book):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{strongFailure}( P ) \cr 2 & \quad F \gets \mathrm{failure}( P );\ G[ 0 ] \gets -1 \cr 3 & \quad \textbf{for}\ q \gets 1\ \textbf{to}\ m\ \textbf{do} \cr 4 & \qquad \textbf{if}\ q < m\ \textbf{and}\ F[ q ] \ge 0\ \textbf{and} \ P[ F[ q ] ] = P[ q ]\ \textbf{then}\ G[ q ] \gets G[ F[ q ] ] \cr 5 & \qquad \textbf{else}\ G[ q ] \gets F[ q ] \cr 6 & \quad \textbf{return}\ G \end{array}} \]

Postcondition
\( G[ q ] \le F[ q ] \)

◆ twoWay()

std::vector< std::size_t > ADS::twoWay ( std::string_view T,
std::string_view P )
inline

the places of all the occurrences of \( P \) in \( T \), by the Two-Way algorithm of Crochemore and Perrin

In pseudocode (Crochemore and Perrin, beyond the book), the critical factorization \( P = P[ 0 .. \ell ] \, P[ \ell + 1 .. m - 1 ] \) with the period \( p \), the right part compared first, and \( \mu \) the prefix of \( P \) known to match after a shift by the period of a periodic pattern (\( -1 \) otherwise):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{twoWay}( T , P ) \cr 2 & \quad ( \ell , p ) \gets \text{the later of the maximal suffixes of } P \text{ for an order and its reverse, with its period} \cr 3 & \quad \mathit{periodic} \gets P[ 0 .. \ell ] = P[ p .. p + \ell ] \cr 4 & \quad \textbf{if not}\ \mathit{periodic}\ \textbf{then}\ p \gets \max\{ \ell + 1 , m - \ell - 1 \} + 1 \cr 5 & \quad j \gets 0;\ \mu \gets -1;\ R \gets \emptyset \cr 6 & \quad \textbf{while}\ j \le n - m\ \textbf{do} \cr 7 & \qquad i \gets \max\{ \ell , \mu \} + 1 \cr 8 & \qquad \textbf{while}\ i < m\ \textbf{and}\ P[ i ] = T[ i + j ] \ \textbf{do}\ i \gets i + 1 \cr 9 & \qquad \textbf{if}\ i < m\ \textbf{then}\ j \gets j + i - \ell;\ \mu \gets -1 \cr 10 & \qquad \textbf{else} \cr 11 & \qquad\quad i \gets \ell \cr 12 & \qquad\quad \textbf{while}\ i > \mu\ \textbf{and}\ P[ i ] = T[ i + j ]\ \textbf{do}\ i \gets i - 1 \cr 13 & \qquad\quad \textbf{if}\ i \le \mu\ \textbf{then}\ R \gets R \cup \{ j \} \cr 14 & \qquad\quad j \gets j + p \cr 15 & \qquad\quad \textbf{if}\ \mathit{periodic}\ \textbf{then}\ \mu \gets m - p - 1 \cr 16 & \quad \textbf{return}\ R \end{array}} \]

Postcondition
the occurrences of naiveMatching( T , P ) (audit)

◆ zFunction()

std::vector< std::size_t > ADS::zFunction ( std::string_view S)
inline

the Z-function: \( Z[ i ] \) is the length of the longest common prefix of \( S \) and \( S[ i .. ] \), and \( Z[ 0 ] = |S| \)

In pseudocode (Gusfield, section 1.4, beyond the book), \( [ l , r ) \) the Z-box that reaches farthest, i.e., the one with \( S[ l .. r - 1 ] = S[ 0 .. r - l - 1 ] \); each comparison that succeeds moves \( r \) forward, hence \( \Theta( n ) \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{zFunction}( S ) \cr 2 & \quad Z[ 0 ] \gets n;\ l \gets 0;\ r \gets 0 \cr 3 & \quad \textbf{for}\ i \gets 1\ \textbf{to}\ n - 1\ \textbf{do} \cr 4 & \qquad \textbf{if}\ i < r\ \textbf{then}\ k \gets \min\{ Z[ i - l ] , r - i \}\ \textbf{else}\ k \gets 0 \cr 5 & \qquad \textbf{while}\ i + k < n\ \textbf{and}\ S[ k ] = S[ i + k ] \ \textbf{do}\ k \gets k + 1 \cr 6 & \qquad Z[ i ] \gets k \cr 7 & \qquad \textbf{if}\ i + k > r\ \textbf{then}\ l \gets i;\ r \gets i + k \cr 8 & \quad \textbf{return}\ Z \end{array}} \]

Postcondition
\( Z[ i ] \le |S| - i \)

◆ zMatching()

std::vector< std::size_t > ADS::zMatching ( std::string_view T,
std::string_view P )
inline

the places of all the occurrences of \( P \) in \( T \), by the Z-function of \( P \) and the Z-boxes of \( T \)

In pseudocode (Gusfield, section 1.4, beyond the book), the box \( [ l , r ) \) of \( T \) with \( T[ l .. r - 1 ] = P[ 0 .. r - l - 1 ] \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{zMatching}( T , P ) \cr 2 & \quad Z \gets \mathrm{zFunction}( P );\ l \gets 0;\ r \gets 0;\ R \gets \emptyset \cr 3 & \quad \textbf{for}\ i \gets 0\ \textbf{to}\ n - 1\ \textbf{do} \cr 4 & \qquad \textbf{if}\ i < r\ \textbf{then}\ k \gets \min\{ Z[ i - l ] , r - i \}\ \textbf{else}\ k \gets 0 \cr 5 & \qquad \textbf{while}\ i + k < n\ \textbf{and}\ k < m\ \textbf{and} \ T[ i + k ] = P[ k ]\ \textbf{do}\ k \gets k + 1 \cr 6 & \qquad \textbf{if}\ i + k > r\ \textbf{then}\ l \gets i;\ r \gets i + k \cr 7 & \qquad \textbf{if}\ k = m\ \textbf{then}\ R \gets R \cup \{ i \} \cr 8 & \quad \textbf{return}\ R \end{array}} \]

Postcondition
the occurrences of naiveMatching( T , P ) (audit)