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

The anagrams (exercise 16.8). More...

Files

file  ADS/backtrack/Anagrams.h
 The anagrams (exercise 16.8): the strings made of all the characters of a given one, in any order, that split into words of a dictionary, written with spaces between the words.

Functions

template<class W, class P>
std::set< std::string > ADS::anagrams (const std::string &s, W &&word, P &&prefix)
 the anagrams of \( s \) made of words for which word( w ) is true, prefix( w ) being true if \( w \) begins some word (a word included); the words of a sentence are separated by one blank, and the empty \( s \) has the one anagram ""

Detailed description

The anagrams (exercise 16.8).

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

Function documentation

◆ anagrams()

template<class W, class P>
std::set< std::string > ADS::anagrams ( const std::string & s,
W && word,
P && prefix )

the anagrams of \( s \) made of words for which word( w ) is true, prefix( w ) being true if \( w \) begins some word (a word included); the words of a sentence are separated by one blank, and the empty \( s \) has the one anagram ""

In pseudocode (exercise 16.8), \( L \) the multiset of the characters of \( s \) not placed yet, \( w \) the word being built and \( T \) the words closed so far:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{anagrams}( L , w , T )\quad \triangleright\ L \text{ the letters left, } w \text{ the word open, } T \text{ the words closed} \cr 2 & \quad \textbf{if}\ L = \emptyset\ \textbf{then}\ \text{record } T \text{ if } w = \varepsilon;\ \textbf{return}\quad \triangleright \ \text{a half word is no anagram} \cr 3 & \quad \textbf{foreach}\ \text{letter } c \text{ of } L \text{, each value once, in increasing order}\ \textbf{do} \cr 4 & \qquad \text{move } c \text{ from } L \text{ to the end of } w \cr 5 & \qquad \textbf{if}\ \mathrm{prefix}( w )\ \textbf{then}\quad \triangleright\ \text{else no word starts with } w \cr 6 & \qquad\quad \mathrm{anagrams}( L , w , T )\quad \triangleright\ w \text{ grows} \cr 7 & \qquad\quad \textbf{if}\ \mathrm{word}( w )\ \textbf{then} \ \mathrm{anagrams}( L , \varepsilon , T \cdot w )\quad \triangleright\ w \text{ is closed} \cr 8 & \qquad \text{move the last letter of } w \text{ back to } L \end{array}} \]

Postcondition
each sentence has the letters of \( s \) (audit)