My function outputLadder function basically takes in a start and end string and generates the shortest word ladder from that.
It is iterating through a std::list containing over 3600 words read into it from a text file and seems to take a very long time.
What can I do to make my function faster while still maintaining a stack and queue implementation of the word ladder?
Here is the link to what the text file contains: http://textuploader.com/dkacm
Here is a video of the output which shows how long it takes.
Measuring it with clock() it takes about 8 seconds to find the shortest word ladder for style and crazy.
void WordLadder::outputLadder(const string &start, const string &end)
{
stack<string> words;
words.push(start);
queue<stack<string>> ladder;
ladder.push(words);
while (!ladder.empty())
{
for (list<string>::iterator i = dictionary.begin(); i != dictionary.end(); ++i)
{
if (oneDiff(*i, ladder.front().top()))
{
if (*i == end)
{
stack<string> output;
while (!ladder.front().empty())
{
output.push(ladder.front().top());
ladder.front().pop();
}
while (!output.empty())
{
cout << output.top() << " ";
output.pop();
}
cout << *i << endl;
return;
}
else
{
stack<string> temp = ladder.front();
temp.push(*i);
ladder.push(temp);
i = dictionary.erase(i);
}
}
}
ladder.pop();
}
cout << "No word ladder exists for this word." << endl;
}
bool WordLadder::oneDiff(const string &dictWord, const string &stackWord)
{
int count = 0;
for (int i = 0; i < stackWord.size(); ++i)
{
if (dictWord.at(i) != stackWord.at(i))
{
++count;
}
}
if (count <= 1)
{
return true;
}
return false;
}