/* * main.cpp * * Created on: Oct 18, 2010 * Author: mike * for use in Yale course CPSC 427a, Fall 2010 */ /* Triangle word puzzle * Copyright 1995 by Michael J. Fischer * Solves challenge #85, "Pyramid", in "The IQ Challenge", * Philip J. Carter and Ken H. Russell, Barnes & Noble Books, 1993, * ISBN 1-56619-164-5 * usage: triangle [dictionary] * * Uses specified dictionary, or /usr/dict/words if no dictionay * specified. Looks for words containing only the letters in * allowed_chars. Tries to find a triangle of allowed words so that * each word is an anagram of the next shorter word plus one new * allowed letter. * Method: Reads in dictionary file. Throws out disallowed words. * Builds a table of WordLists, where each WordList is a chain of * WordEntry objects for words of a given length. Each WordEntry * contains a profile consisting of the letters of the word in sorted * order. This enables two words to be quickly compared to see if one * is an anagram of a subword of the other. A second pass over the * dictionary discards each word that is not an anagram of some next * shorter word plus a new letter. It also sets a parent pointer in * each word to the shorter word from which it is derived. A final * pass prints the triangle for each word of length triangle_max. */ #include #include #include #include "word.hpp" #include "wordlist.hpp" #include "dict.hpp" #include "triangle.hpp" /* constants */ #define DICTIONARY_FILE "/usr/share/dict/words" static void usage(const char* prog) { cout << "usage: " << prog << " [dictionary [filter [min_size [max_size]]]]" << endl; } // -------------------------------------------------------------- int main(int argc, char *argv[]) { const char *prog; const char *dictname; int c; banner(); /* check args */ prog = argv[0]; dictname = (argc <= 1) ? DICTIONARY_FILE : argv[1]; Word::allowed_chars = (argc <= 2) ? "abcdefghijklmnopqrstuvwxyz" : argv[2]; Word::triangle_min = (argc <= 3) ? MINLEN : strtol(argv[3], NULL, 0); Word::triangle_max = (argc <= 4) ? MAXLEN : strtol(argv[4], NULL, 0); if (argc > 5) { usage(prog); return 1; } if (Word::triangle_min > Word::triangle_max) { cout << "min_size " << Word::triangle_min << " should be at most max_size " << Word::triangle_max << endl; usage(prog); return 2; } if (Word::triangle_max > MAXLEN) { cout << "Can't handle triangles larger than " << MAXLEN << endl; usage(prog); return 2; } cout << "Finding size " << Word::triangle_max << " triangles\n"; cout << " using dictionary " << dictname << "\n"; cout << " with filter \"" << Word::allowed_chars << "\"\n" << endl; /* initialize character type table */ for (c = 0; c < 256; c++) Word::allowed_set[c] = false; for (unsigned int k = 0; k < strlen(Word::allowed_chars); k++) { c = Word::allowed_chars[k]; Word::allowed_set[c] = true; } /* read and store dictionary */ Dictionary dict; dict.readDict(dictname); /* build triangle */ cout << "Building triangle data structure" << endl; Triangle tri; tri.build(dict); /* print out triangles found */ cout << "Some triangles" << endl; tri.print(cout); bye(); }