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

The number of ways of writing a sequence \( X \) of \( n \) items as a concatenation of words, the counting version of the word break problem. More...

Files

file  ADS/string/Segmentations.h
 The number of ways of writing a sequence \( X \) of \( n \) items as a concatenation of words, the counting version of the word break problem [see WordBreak.h, which finds one of them].

Functions

template<class S>
requires std::is_class_v< S >
std::uint64_t ADS::countSegmentations (const S &X, const std::vector< S > &words)
 the number of ways of writing \( X \) as a sequence of the words, modulo \( 2^{64} \); a word repeated in the list counts once for each copy; 1 for the empty \( X \)
std::uint64_t ADS::countSegmentations (const std::string &X, const std::vector< std::string > &words)
 the count of countSegmentations() above, on std::string (or literals)
std::uint64_t ADS::countDecodings (const std::vector< int > &digits)
 the number of readings of the digits as letters \( A = 1 , \ldots , Z = 26 \), modulo \( 2^{64} \); 1 for no digits

Detailed description

The number of ways of writing a sequence \( X \) of \( n \) items as a concatenation of words, the counting version of the word break problem.

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

Function documentation

◆ countDecodings()

std::uint64_t ADS::countDecodings ( const std::vector< int > & digits)
inlinenodiscard

the number of readings of the digits as letters \( A = 1 , \ldots , Z = 26 \), modulo \( 2^{64} \); 1 for no digits

In pseudocode (chapter 13), \( \mathit{next} = D[ i + 1 ] \) and \( \mathit{after} = D[ i + 2 ] \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{countDecodings}( d )\quad \triangleright \ \mathit{next} , \mathit{after} \text{ the counts of the suffixes from } i + 1 , i + 2 \cr 2 & \quad \mathit{next} \gets 1;\ \mathit{after} \gets 0 \cr 3 & \quad \textbf{for}\ i \gets n - 1\ \textbf{downto}\ 0\ \textbf{do} \cr 4 & \qquad h \gets 0\quad \triangleright\ \text{a } 0 \text{ starts no letter} \cr 5 & \qquad \textbf{if}\ d_i \ne 0\ \textbf{then}\ h \gets \mathit{next} + ( \mathit{after} \text{ if } i + 1 < n \text{ and } 10 d_i + d_{i+1} \le 26 \text{, else } 0 ) \cr 6 & \qquad ( \mathit{after} , \mathit{next} ) \gets ( \mathit{next} , h ) \cr 7 & \quad \textbf{return}\ \mathit{next} \end{array}} \]

Precondition
every digit is in \( 0 .. 9 \)

◆ countSegmentations() [1/2]

template<class S>
requires std::is_class_v< S >
std::uint64_t ADS::countSegmentations ( const S & X,
const std::vector< S > & words )
nodiscard

the number of ways of writing \( X \) as a sequence of the words, modulo \( 2^{64} \); a word repeated in the list counts once for each copy; 1 for the empty \( X \)

In pseudocode (chapter 13), the table filled from the right:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{countSegmentations}( X , W )\quad \triangleright\ D[ i ] \text{ the segmentations of } X[ i .. n ) \cr 2 & \quad D[ n ] \gets 1 \cr 3 & \quad \textbf{for}\ i \gets n - 1\ \textbf{downto}\ 0\ \textbf{do} \ D[ i ] \gets \textstyle\sum \{ D[ i + |w| ] : w \in W \text{, } w \text{ a prefix of } X[ i .. n ) \} \cr 4 & \quad \textbf{return}\ D[ 0 ] \end{array}} \]

Precondition
no word is empty

◆ countSegmentations() [2/2]

std::uint64_t ADS::countSegmentations ( const std::string & X,
const std::vector< std::string > & words )
inlinenodiscard

the count of countSegmentations() above, on std::string (or literals)