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

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]. More...

#include "ADS/Contract.h"
#include <cstddef>
#include <cstdint>
#include <string>
#include <type_traits>
#include <vector>
#include "ADS/string/impl/Segmentations.tpp"

Go to the source code of this file.

Namespaces

namespace  ADS

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 [see WordBreak.h, which finds one of them].

  • countSegmentations(): \( D[ i ] \), the number of segmentations of \( X[ i .. n ) \), is the sum of \( D[ i + |w| ] \) over the words \( w \) of the list that occur in \( X \) at \( i \), and \( D[ n ] = 1 \), filled from the right in \( O( n \sum_w |w| ) \) time and \( O( n ) \) space. With the automaton of Aho and Corasick on the words, the occurrences ending at each place come in one scan, and the time drops to \( O( n + \sum_w |w| + \mathit{occ} ) \) for \( \mathit{occ} \) occurrences.
  • countDecodings(): the readings of a string of digits as letters, \( A = 1 , \ldots , Z = 26 \), i.e., the segmentations into the words \( 1 , \ldots , 26 \); \( D[ i ] = 0 \) if \( d_i = 0 \), else \( D[ i + 1 ] \) plus \( D[ i + 2 ] \) if \( d_i d_{i+1} \) is in \( 10 .. 26 \), in \( \Theta( n ) \) time and \( O( 1 ) \) space, the two last entries of \( D \). On \( n \) digits 1 the count is the Fibonacci number \( F_{n+1} \).

The counts grow exponentially with \( n \), and they are given modulo \( 2^{64} \). countSegmentations() takes any sequence S with size(), operator[] and the comparison of its items; the overload on std::string also takes string literals.

The contracts [see Contract.h]: no word is empty, and the digits are in \( 0 .. 9 \). The functions are defined in Segmentations.tpp.

See also
A. Montresor, Algoritmi e Strutture Dati, 2025, chapter 13, https://algoritmicamente.it/materiali/libro/capitoli/cap13.pdf
A. V. Aho, M. J. Corasick, Efficient string matching: an aid to bibliographic search, Communications of the ACM 18(6), 333-340, 1975, doi:10.1145/360825.360855
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa