void wordChain(string file, string startWord) {
map<string, string> first2Letters;
vector<string> dictionary;
string word;
ifstream inputFile(file);
if (inputFile) {
while (inputFile >> word) {
string firstTwo = word.substr(0, 2);
first2Letters.insert(pair<string, string> {firstTwo, word});
dictionary.push_back(word);
}
}
inputFile.close();
cout << startWord << endl;
for (auto w : dictionary) {
string lastTwo = startWord.substr(startWord.length() - 2);
map<string, string>::iterator iter;
iter = first2Letters.find(lastTwo);
if (iter != first2Letters.end()) {
string toRemove = startWord;
dictionary.erase(remove(dictionary.begin(), dictionary.end(), toRemove), dictionary.end());
startWord = first2Letters[lastTwo];
cout << startWord << endl;
}
}
}
Comments