ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ADS::SuffixAutomaton Class Reference

the suffix automaton of a string, built a byte at a time More...

#include <ADS/string/SuffixAutomaton.h>

Public member functions

 SuffixAutomaton (void)
 the automaton of the empty string, the start state alone
 SuffixAutomaton (std::string_view S)
 the automaton of \( S \), by extend() on each of its bytes
void extend (unsigned char c)
 the automaton of \( S c \) from that of \( S \)
bool contains (std::string_view P) const
 whether \( P \) is a substring of \( S \), by following \( P \) from the start
std::size_t occurrences (std::string_view P) const
 the number of the occurrences of \( P \) in \( S \) (\( n + 1 \) for the empty \( P \))
std::size_t distinctSubstrings (void) const
 the number of the nonempty distinct substrings of \( S \), \( \sum_{v \ne \mathit{start}} ( \mathit{len}( v ) - \mathit{len}( \mathit{link}( v ) ) ) \)
std::pair< std::size_t, std::size_t > longestCommonSubstring (std::string_view T) const
 the length of a longest common substring of \( S \) and \( T \), and where it ends in \( T \) (one past its last byte), the first one in \( T \)
std::size_t states (void) const
 the number of states
std::size_t size (void) const
 the length \( n \) of \( S \)

Detailed description

the suffix automaton of a string, built a byte at a time

Constructor and destructor documentation

◆ SuffixAutomaton() [1/2]

ADS::SuffixAutomaton::SuffixAutomaton ( void )

the automaton of the empty string, the start state alone

◆ SuffixAutomaton() [2/2]

ADS::SuffixAutomaton::SuffixAutomaton ( std::string_view S)
explicit

the automaton of \( S \), by extend() on each of its bytes

Member function documentation

◆ contains()

bool ADS::SuffixAutomaton::contains ( std::string_view P) const
nodiscard

whether \( P \) is a substring of \( S \), by following \( P \) from the start

◆ distinctSubstrings()

std::size_t ADS::SuffixAutomaton::distinctSubstrings ( void ) const
nodiscard

the number of the nonempty distinct substrings of \( S \), \( \sum_{v \ne \mathit{start}} ( \mathit{len}( v ) - \mathit{len}( \mathit{link}( v ) ) ) \)

◆ extend()

void ADS::SuffixAutomaton::extend ( unsigned char c)

the automaton of \( S c \) from that of \( S \)

In pseudocode (the construction of Blumer et al., beyond the book), \( \mathit{last} \) the state of \( S \) and \( \delta( p , c ) \) the transition of \( p \) on \( c \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{extend}( c ) \cr 2 & \quad \mathit{cur} \gets \text{a new state};\ \mathit{len}( \mathit{cur} ) \gets \mathit{len}( \mathit{last} ) + 1; \ \mathit{link}( \mathit{cur} ) \gets \mathit{start} \cr 3 & \quad p \gets \mathit{last} \cr 4 & \quad \textbf{while}\ p \ne \textbf{nil}\ \textbf{and}\ \delta( p , c ) = \textbf{nil}\ \textbf{do}\ \delta( p , c ) \gets \mathit{cur};\ p \gets \mathit{link}( p ) \cr 5 & \quad \textbf{if}\ p \ne \textbf{nil}\ \textbf{then} \cr 6 & \qquad q \gets \delta( p , c ) \cr 7 & \qquad \textbf{if}\ \mathit{len}( q ) = \mathit{len}( p ) + 1 \ \textbf{then}\ \mathit{link}( \mathit{cur} ) \gets q \cr 8 & \qquad \textbf{else} \cr 9 & \qquad\quad r \gets \text{a copy of } q \text{ with } \mathit{len}( r ) = \mathit{len}( p ) + 1 \cr 10 & \qquad\quad \textbf{while}\ p \ne \textbf{nil}\ \textbf{and} \ \delta( p , c ) = q\ \textbf{do}\ \delta( p , c ) \gets r;\ p \gets \mathit{link}( p ) \cr 11 & \qquad\quad \mathit{link}( q ) \gets r;\ \mathit{link}( \mathit{cur} ) \gets r \cr 12 & \quad \mathit{last} \gets \mathit{cur} \end{array}} \]

Postcondition
at most \( 2 |S c| \) states (an invariant)

◆ longestCommonSubstring()

std::pair< std::size_t, std::size_t > ADS::SuffixAutomaton::longestCommonSubstring ( std::string_view T) const
nodiscard

the length of a longest common substring of \( S \) and \( T \), and where it ends in \( T \) (one past its last byte), the first one in \( T \)

In pseudocode (beyond the book), \( l \) the length of the longest suffix of \( T[ 0 .. j ] \) that is a substring of \( S \) and \( v \) its state:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{longestCommonSubstring}( T ) \cr 2 & \quad v \gets \mathit{start};\ l \gets 0;\ \mathit{best} \gets 0; \ \mathit{end} \gets 0 \cr 3 & \quad \textbf{for}\ j \gets 0\ \textbf{to}\ |T| - 1\ \textbf{do} \cr 4 & \qquad \textbf{while}\ v \ne \mathit{start}\ \textbf{and}\ \delta( v , T[ j ] ) = \textbf{nil}\ \textbf{do}\ v \gets \mathit{link}( v );\ l \gets \mathit{len}( v ) \cr 5 & \qquad \textbf{if}\ \delta( v , T[ j ] ) \ne \textbf{nil} \ \textbf{then}\ v \gets \delta( v , T[ j ] );\ l \gets l + 1 \cr 6 & \qquad \textbf{if}\ l > \mathit{best}\ \textbf{then}\ \mathit{best} \gets l;\ \mathit{end} \gets j + 1 \cr 7 & \quad \textbf{return}\ ( \mathit{best} , \mathit{end} ) \end{array}} \]

◆ occurrences()

std::size_t ADS::SuffixAutomaton::occurrences ( std::string_view P) const
nodiscard

the number of the occurrences of \( P \) in \( S \) (\( n + 1 \) for the empty \( P \))

In pseudocode (beyond the book), the counts made once after the last extension:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{occurrences}( P ) \cr 2 & \quad \textbf{if}\ P \text{ is empty}\ \textbf{then return}\ n + 1 \cr 3 & \quad v \gets \text{the state reached from the start by } P; \ \textbf{if}\ v = \textbf{nil}\ \textbf{then return}\ 0 \cr 4 & \quad \textbf{if}\ \text{the counts are not made}\ \textbf{then} \cr 5 & \qquad \mathit{cnt}( u ) \gets 1 \text{ for the states that end a } \text{prefix of } S \text{, 0 for the clones} \cr 6 & \qquad \textbf{foreach}\ u \ne \mathit{start} \text{ by decreasing } \mathit{len}( u )\ \textbf{do}\ \mathit{cnt}( \mathit{link}( u ) ) \gets \mathit{cnt}( \mathit{link}( u ) ) + \mathit{cnt}( u ) \cr 7 & \quad \textbf{return}\ \mathit{cnt}( v ) \end{array}} \]

◆ size()

std::size_t ADS::SuffixAutomaton::size ( void ) const
inlinenodiscard

the length \( n \) of \( S \)

◆ states()

std::size_t ADS::SuffixAutomaton::states ( void ) const
inlinenodiscard

the number of states


The documentation for this class was generated from the following file: