* This function lists all of the mnemonics for the string of digits
* stored in the string str. The correspondence between digits and
* letters is the same as that on the standard telephone dial. The
* implementation at this level is a simple wrapper function that
* provides the arguments necessary for the recursive call.
*/
void ListMnemonics(string str){
* This function does all of the real work for ListMnemonics and
* implements a more general problem with a recursive solution
* that is easier to see. The call to RecursiveMnemonics generates
* all mnemonics for the digits in the string rest prefixed by the
* mnemonic string in prefix. As the recursion proceeds, the rest
* string gets shorter and the prefix string gets longer.
*/
void RecursiveMnemonics(string prefix, string rest){
if (rest.length() == 0){
}else{
string options = DigitLetters(rest[0]);
for (int i = 0; i < options.length(); i++)
* This function returns a string consisting of the legal
* substitutions for a given digit character. Note that 0 and
* 1 are handled just by leaving that digit in its position.
*/
string DigitLetters(char ch){
switch (ch) {
case '0': return ("0");
case '1': return ("1");
case '2': return ("ABC");
case '3': return ("DEF");
case '4': return ("GHI");
case '5': return ("JKL");
case '6': return ("MNO");
case '7': return ("PRS");
case '8': return ("TUV");
case '9': return ("WXY");
default: Error("Illegal digit");
* This function looks for the length of the shortest path through the
* maze starting at the specified position pt. If none exists it
* returns NoSolution, otherwise it returns the shortest distance.
** To find the length of the shortest path, the function computes the
* length of the shortest path from all directions around the
* point. The shortest path is then one more than the smallest of
* these (since it takes one step to move in the given direction). The
* base cases occurs when you are outside the maze, in which case the
* shortest path has zero length, or if you have already been to a
int shortest, len;
if (OutsideMaze(pt))return 0;
if (IsMarked(pt))return NoSolution;
shortest = NoSolution;
MarkSquare(pt);
if (!WallExists(pt, dir)){
len = ShortestPathLength(AdjacentPoint(pt, dir));
if (len < shortest)
* Function returns the minimum value of a, b and c.
*/
int Min(int a, int b, int c) {
if (a < b){
if (a < c) return a;
else return c;
* This function computes the edit distance between the two
* passed in parameters. The edit distance is the minimum number of
* insertions, deletions or substitutions required to transform
* s in to t. To compute this we have two base cases.
* If the strings are equal, then we know the minimum distance is
* 0 since they are the same. If either string is empty, then we have
* an edit distance equal to the length of the other string\u2013 since it
* will take that many insertions to create the other string.
** Otherwise, if the two strings share the same first character, then
* distance is just the minimum of the edit distance between the
* strings created by performing a single substitution, insertion or
* deletion to turn s in to t.
*/
int EditDistance(string s, string t)
{
if (s == t) return 0;
if (s == "") return t.length();
if (t == "") return s.length();
if (s[0] == t[0]){
return 1 + Min(EditDistance (s.substr(1), t.substr(1)),
EditDistance (s.substr(1), t),
EditDistance (s, t.substr(1)));
Leave a Comment