|
ADS
Algorithms and Data Structures
|
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) | |
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.