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

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

#include "ADS/Contract.h"
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <iterator>
#include <set>
#include <type_traits>
#include <utility>
#include <vector>
#include "ADS/string/impl/StringBreaking.tpp"

Go to the source code of this file.

Namespaces

namespace  ADS

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: 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.

With the sentinels \( w_0 = 0 \) and \( w_{n+1} = L \), the value \( D[ i ][ j ] \), the least cost of the cuts strictly between \( w_i \) and \( w_j \), is 0 if \( j = i + 1 \) and otherwise \( w_j - w_i + \min_{i < k < j} ( D[ i ][ k ] + D[ k ][ j ] ) \), the scheme of the chain of matrices and of the optimal search tree, which takes \( \Theta( n^3 ) \) time. The cost \( w_j - w_i \) satisfies the quadrangle inequality as an equality and is monotone on the intervals, so the largest optimal split \( K[ i ][ j ] \) lies between \( K[ i ][ j - 1 ] \) and \( K[ i + 1 ][ j ] \) (Knuth for the search trees, Yao in general): the candidates of all the intervals of one length add up to \( O( n ) \), and the table costs \( \Theta( n^2 ) \) time and space. stringBreakingOrder() reads an optimal order back from the splits, each cut before those of its two pieces.

The cut places are any integer type; the overloads on std::int64_t also take a braced list.

The contracts [see Contract.h]: the places are strictly increasing and inside \( ( 0 , L ) \); the order is a permutation of the places whose cost is the least one (an audit). The functions are defined in StringBreaking.tpp.

See also
A. Montresor, Algoritmi e Strutture Dati, 2025, chapter 13, https://algoritmicamente.it/materiali/libro/capitoli/cap13.pdf
T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein, Introduction to Algorithms, 3rd ed., MIT Press, 2009, problem 15-9
D. E. Knuth, Optimum binary search trees, Acta Informatica 1(1), 14-25, 1971, doi:10.1007/BF00264289
F. F. Yao, Efficient dynamic programming using quadrangle inequalities, Proceedings of the 12th ACM Symposium on Theory of Computing, 429-435, 1980, doi:10.1145/800141.804691
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa