patterncppMinor
Longest words in dictionary that can be constructed from a list of letters
Viewed 0 times
fromcanwordslistthatconstructeddictionarylongestletters
Problem
The problem is to find the longest words in a dictionary of legal words that can be constructed from a given list of letters.
For example:
More details here.
I've already solved this problem in Python (see this).
To brush up my C++ I decided to reproduce the exact same algorithm in C++. I learned C++ as "C with classes" and never learned to use the STL properly, so I usually end up writing more code than necessary to accomplish simple things.
This is what I came up with for this problem:
For example:
$ ./scrabble dictionary.txt i g h l p r a
argil glair grail graph hilar laigh phial pilar ralphMore details here.
I've already solved this problem in Python (see this).
To brush up my C++ I decided to reproduce the exact same algorithm in C++. I learned C++ as "C with classes" and never learned to use the STL properly, so I usually end up writing more code than necessary to accomplish simple things.
This is what I came up with for this problem:
#include
#include
#include
#include
using namespace std;
bool can_make(string word, vector letters);
int get_max_len(vector words);
int main(int argc, char* argv[])
{
// Grab the dictionary.
fstream dict_file(argv[1]);
string in;
vector dict_words;
while(getline(dict_file, in)) dict_words.push_back(in);
dict_file.close();
// Get the list of input letters.
vector letters;
for (int i = 2; i all_words;
vector::iterator it;
for (it = dict_words.begin(); it longest_words;
for (it = all_words.begin(); it letters)
{
/*
Return true if the word can be generated by all letters in
and only the letters in .
*/
if (word.length() > letters.size()) return false;
vector l(letters);
vector word_letters(word.c_str(), word.c_str()+word.length());
vector::iterator it, loc;
// Iterate through . If a letter in also
// appears in , remove it. Otherwise, return false.
for (it = word_letters.begin(); it words)
{
/*
Return the length of the longest word(s) in .
*/
int max_len = 0;
vector::iterator it;
for (it = words.begin(); it max_len)
max_len = (*it).length();
return max_len;
}- Is this idiomatic C++?
- How can I make this code more concise?
- How can I make it more correct?
- What C++11 features can I use here to make th
Solution
Preface
I am not sure the algorithm you have chosen is the best one. Sorting the letters, or using a frequency table of the letters, are obvious alternatives. However, this is code review, and not algorithm review.
So here we go:
Iterators
Don't use `
I am not sure the algorithm you have chosen is the best one. Sorting the letters, or using a frequency table of the letters, are obvious alternatives. However, this is code review, and not algorithm review.
So here we go:
Iterators
Don't use `
is an example where - There is no need for
dict_file.close()as the destructor will close the file at the right time.
Code Snippets
for (it = dict_words.begin(); it < dict_words.end(); it++)for (it = dict_words.begin(); it != dict_words.end(); ++it)vector<string>::iterator it;
for (it = dict_words.begin(); it != dict_words.end(); ++it)for (auto it = dict_words.begin(); it != dict_words.end(); ++it)for (auto it : dict_words)Context
StackExchange Code Review Q#6634, answer score: 8
Revisions (0)
No revisions yet.