c++ - 以相反的顺序显示内容
问题描述
下面的代码是由一位溢出者向我建议的。所以学分不是我的。我试图绕过这段代码并尝试以相反的顺序打印出元素。到目前为止,这些元素都是从起始词 dog 开始打印出来的。但目标是以另一种方式打印它。从猫开始。因此,基本上,代码可以根据每个人的祖先来回溯单词。例如,在这种情况下,我们从 cag 中得到 cat,它是祖先,而 cag 的祖先是 cog。依此类推,直到我们从狗开始
#include <iostream>
#include <string>
#include <unordered_set>
#include <stack>
#include <vector>
using namespace std;
int main() {
vector<string> dictionary;
vector<pair<string, int>> words; //stores (word, predecessor)
string startWord = "dog";
string endWord = "cat";
unordered_set<string> seenWords;
dictionary.push_back("dog");
dictionary.push_back("bog");
dictionary.push_back("cog");
dictionary.push_back("fog");
dictionary.push_back("cat");
dictionary.push_back("bag");
dictionary.push_back("beg");
dictionary.push_back("bet");
dictionary.push_back("bat");
words.emplace_back(startWord, -1);
seenWords.insert(startWord);
bool found = false;
//Try all new words as reference words
for(int i = 0; i < words.size() && !found; ++i) {
//we look for words that we can generate from words[i]
cout << i << " " << words[i].first << ": ";
//try all the words from the dictionary
for (int j = 0; j < dictionary.size(); j++) {
string& candidate = dictionary[j];
//check if candidate can be generated from reference
//count the different characters
int differentCharacters = 0;
for (int pos = 0; pos < words[i].first.size(); ++pos)
{
if (candidate[pos] != words[i].first[pos])
++differentCharacters;
}
if (differentCharacters == 1 && seenWords.find(candidate) == seenWords.end()) {
//yes, we can generate this candidate from word[i] and we haven't seen the word before
cout << "(" << words.size() << ")" << candidate << " ";
words.emplace_back(candidate, i);
seenWords.insert(candidate);
if (candidate == endWord) {
found = true;
cout << "Found endword";
break;
}
}
}
cout << endl;
}
if (found) {
//traverse the word path from the end word back to the start word
int i = words.size() - 1;
stack<string> wordPath;
while (i != -1) {
//push the current word onto a stack
wordPath.push(words[i].first);
//go to the previous word
i = words[i].second;
}
//now retrieve the words from the stack and print them in reverse order
cout << "Word path:" << endl;
while (!wordPath.empty()) {
cout << wordPath.top() << " ";
wordPath.pop();
}
cout << endl;
}
return EXIT_SUCCESS;
}
解决方案
它实际上非常简单!stack
与其使用 a推送然后弹出您的“找到”字符串路径,不如使用avector
和push_back
字符串;然后您可以按任意顺序打印值!在这段代码中,我已经从你所拥有的切换到“其他”顺序:
if (found) {
//traverse the word path from the end word back to the start word
int i = words.size() - 1;
/// stack<string> wordPath;
vector<string> wordPath;
while (i != -1) {
// push the current word into a vector ...
/// wordPath.push(words[i].first);
wordPath.push_back(words[i].first);
//go to the previous word
i = words[i].second;
}
// now retrieve the words from the vector and print them ...
cout << "Word path:" << endl;
/// while (!wordPath.empty()) {
/// cout << wordPath.top() << " ";
/// wordPath.pop();
/// }
///
for (size_t w = 0; w < wordPath.size(); ++w) {
string text = wordPath[w];
size_t index = 0;
for (index = 0; index < dictionary.size(); ++index) {
if (text == dictionary[index]) break;
}
cout << text << "[" << index << "] ";
}
///
cout << endl;
}
你甚至可以在这里做出选择!要以原始(='reverse')顺序打印,只需更改for
循环:
for (size_t w = wordPath.size() - 1; w <= 0 ; --w) {
cout << wordPath[w] << " ";
}
推荐阅读
- javascript - 二 .then 在 axios
- android - APK 扩展文件 - 下载器库不支持 Android Oreo 和 Android Studio 3.2 版?
- c - IP_DROP_MEMBERSHIP 行为不一致
- reactjs - redux Form 7.4.2 服务器端验证不适用于 axios ,给出错误 Unhandled Rejection (SubmissionError): Submit Validation Failed
- java - 基于 https 的 SonarQube
- c# - 从拆分器面板中删除后无法显示 winform
- android - Flutter项目Android报错:找不到lint-gradle-api.jar (com.android.tools.lint:lint-gradle-api:26.1.2)
- sql - 如何根据日期表日期
- ios - 如何快速裁剪具有给定角度的图像
- java - 如何从imagebutton获取位图