|
ADS
Algorithms and Data Structures
|
Is it Italian? More...
Files | |
| file | ADS/string/WordBreak.h |
| Is it Italian? | |
Functions | |
| template<class F> | |
| std::optional< std::vector< std::string > > | ADS::wordBreak (const std::string &s, F &&lookup, std::size_t longest=std::string::npos, long *lookups=nullptr) |
| the words of \( s \), if it is a sequence of words, by the lookup( word ) of a dictionary; the lookups made go to *lookups, if given | |
Is it Italian?
The definitions, the invariants and the costs of the area are in Theory of strings.
| std::optional< std::vector< std::string > > ADS::wordBreak | ( | const std::string & | s, |
| F && | lookup, | ||
| std::size_t | longest = std::string::npos, | ||
| long * | lookups = nullptr ) |
the words of \( s \), if it is a sequence of words, by the lookup( word ) of a dictionary; the lookups made go to *lookups, if given
In pseudocode (exercise 13.13), \( \mathit{from}[ i ] \) the start of the last word of \( s[ 0 .. i ) \), \( -1 \) if it is no sequence of words, and \( w \) the length of the longest word:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{wordBreak}( s , \mathrm{lookup} , w )\quad \triangleright\ w \text{ the longest word} \cr 2 & \quad \mathit{from}[ 0 ] \gets 0;\ \mathit{from}[ i ] \gets -1 \text{ for } i > 0\quad \triangleright\ -1 \text{: } s[ 0 .. i ) \text{ cannot be split} \cr 3 & \quad \textbf{for}\ i \gets 1\ \textbf{to}\ n\ \textbf{do} \cr 4 & \qquad \mathit{from}[ i ] \gets \text{the largest } j \in [ \max\{ 0 , i - w \} , i ) \text{ with } \mathit{from}[ j ] \ge 0 \text{ and } \mathrm{lookup}( s[ j .. i ) ) \text{, if any} \cr 5 & \quad \textbf{return}\ \textbf{nil} \text{ if } \mathit{from}[ n ] < 0 \text{, else the words found by following } \mathit{from} \text{ back from } n \end{array}} \]