void LetterTree::insert(std::string word) { using KeyType = std::pair<char, LetterNode>; LetterNode* currentNode = this; LetterMap* nextmap = &following; for(char c : word) { if (!nextmap->count(c)) { auto ln = LetterNode(c, currentNode); nextmap->insert(KeyType(c, ln)); } currentNode = &nextmap->at(c); nextmap = ¤tNode->following; } nextmap->insert(KeyType('\0', EOW)); }
void setLetterValues(LetterMap &letterMap) { short values[] = { 1, 3, 3, 2, 1, 4, 2, 4, 2, 8, 5, 1, 3, 1, 1, 3, 10, 1, 1, 1, 1, 4, 4, 8, 4, 10 }; // values for(char letter = 'a'; letter <= 'z'; letter++) letterMap.insert(LetterMap::value_type(letter, values[letter - 'a'])); } // setLetterValues();
void readWords(const LetterMap &letterMap, WordMap &words) { ifstream inf("words.txt"); string word, sortedWord; short value; while( getline(inf, word)) { if(word.length() <= MAX_WORD_LENGTH && word.find_first_not_of("abcedefghijklmnopqrstuvwxyz") == string::npos) { value = 0; for(string::const_iterator itr = word.begin(); itr != word.end(); itr++) value += (letterMap.find(*itr))->second; words.insert(WordMap::value_type(word, value)); } // if word up to MAX_WORD_LENGTH characters and no captial letters. } // while } // readWords()
void processBlankWord(const WordMap &words, string &originalWord, set<string> &bestWords, short &bestCount, const LetterMap &letterMap, WordMap::const_iterator &itr, char letter) { int count = itr->second; if(std::count(itr->first.begin(), itr->first.end(), letter) > std::count(originalWord.begin(), originalWord.end(), letter)) // space used count -= letterMap.find(letter)->second; if(count >= bestCount) { if(count > bestCount) { bestWords.clear(); bestCount = count; } // if better than those previous bestWords.insert(itr->first); } // if at least as good as previous } // processBlankWord()