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

Breaking a string. More...

Files

file  ADS/string/StringBreaking.h
 Breaking a string: a string of length \( L \) has to be cut at the places \( 0 < c_1 < \cdots < c_n < L \), and each cut costs the length of the piece it splits, so that the order of the cuts decides the total cost.

Functions

template<class I>
I ADS::stringBreakingCost (const std::vector< I > &cuts, std::type_identity_t< I > length)
 the least total cost of the cuts of a string of the given length at the places cuts
std::int64_t ADS::stringBreakingCost (const std::vector< std::int64_t > &cuts, std::int64_t length)
 the cost of stringBreakingCost() above, on std::int64_t (or a braced list)
template<class I>
std::vector< I > ADS::stringBreakingOrder (const std::vector< I > &cuts, std::type_identity_t< I > length)
 the places cuts in an order of least total cost
std::vector< std::int64_t > ADS::stringBreakingOrder (const std::vector< std::int64_t > &cuts, std::int64_t length)
 the order of stringBreakingOrder() above, on std::int64_t (or a braced list)

Detailed description

Breaking a string.

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

Function documentation

◆ stringBreakingCost() [1/2]

template<class I>
I ADS::stringBreakingCost ( const std::vector< I > & cuts,
std::type_identity_t< I > length )
nodiscard

the least total cost of the cuts of a string of the given length at the places cuts

In pseudocode (chapter 13, with the speedup of Knuth and Yao), the intervals by increasing length \( d \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{stringBreakingCost}( c , L ) \cr 2 & \quad w \gets ( 0 , c_1 , \ldots , c_n , L ) \cr 3 & \quad \textbf{for}\ i \gets 0\ \textbf{to}\ n\ \textbf{do}\ D[ i ][ i + 1 ] \gets 0;\ K[ i ][ i + 1 ] \gets i + 1 \cr 4 & \quad \textbf{for}\ d \gets 2\ \textbf{to}\ n + 1\ \textbf{do} \cr 5 & \qquad \textbf{for}\ i \gets 0\ \textbf{to}\ n + 1 - d\ \textbf{do} \cr 6 & \qquad\quad j \gets i + d \cr 7 & \qquad\quad k \gets \text{the largest } k \in [ \max\{ K[ i ][ j - 1 ] , i + 1 \} , \min\{ K[ i + 1 ][ j ] , j - 1 \} ] \text{ of least } D[ i ][ k ] + D[ k ][ j ] \cr 8 & \qquad\quad D[ i ][ j ] \gets D[ i ][ k ] + D[ k ][ j ] + w_j - w_i; \ K[ i ][ j ] \gets k \cr 9 & \quad \textbf{return}\ D[ 0 ][ n + 1 ] \end{array}} \]

Precondition
the cuts are strictly increasing and inside \( ( 0 , \mathit{length} ) \)

◆ stringBreakingCost() [2/2]

std::int64_t ADS::stringBreakingCost ( const std::vector< std::int64_t > & cuts,
std::int64_t length )
inlinenodiscard

the cost of stringBreakingCost() above, on std::int64_t (or a braced list)

◆ stringBreakingOrder() [1/2]

template<class I>
std::vector< I > ADS::stringBreakingOrder ( const std::vector< I > & cuts,
std::type_identity_t< I > length )
nodiscard

the places cuts in an order of least total cost

In pseudocode (chapter 13), the splits \( K \) of stringBreakingCost() read back with a stack, each cut before those of its two pieces:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{stringBreakingOrder}( c , L )\quad \triangleright\ \text{the cuts in the order of an optimal plan} \cr 2 & \quad K \gets \text{the best cut of each piece, by } \mathrm{stringBreakingCost}( c , L );\ O \gets () \cr 3 & \quad T \gets \text{a stack holding the whole string } ( 0 , n + 1 ) \cr 4 & \quad \textbf{while}\ T \ne \emptyset\ \textbf{do} \cr 5 & \qquad ( i , j ) \gets \mathrm{pop}( T );\ \textbf{if}\ j \le i + 1 \ \textbf{then continue}\quad \triangleright\ \text{no cut inside} \cr 6 & \qquad k \gets K[ i ][ j ];\ \text{append } w_k \text{ to } O \cr 7 & \qquad \mathrm{push}( T , ( k , j ) );\ \mathrm{push}( T , ( i , k ) )\quad \triangleright\ \text{the left piece first} \cr 8 & \quad \textbf{return}\ O \end{array}} \]

Precondition
the cuts are strictly increasing and inside \( ( 0 , \mathit{length} ) \)
Postcondition
a permutation of cuts of the cost of stringBreakingCost() (audit)

◆ stringBreakingOrder() [2/2]

std::vector< std::int64_t > ADS::stringBreakingOrder ( const std::vector< std::int64_t > & cuts,
std::int64_t length )
inlinenodiscard

the order of stringBreakingOrder() above, on std::int64_t (or a braced list)