下面的代码是由同一个溢出者建议给我的。因此,积分不是我的。我试图绕过这段代码,然后尝试以相反的顺序打印出元素。到目前为止,这些元素都是从狗的起始单词开始打印出来的。但目标是将其打印为其他方式。从猫开始。因此,从本质上讲,代码的工作方式是根据每个单词的祖先回溯单词。例如,在这种情况下,我们从作为祖先的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;
}
答案 0 :(得分:1)
实际上非常简单!无需使用{
"_id" : ObjectId("5d9f074f5833c8cd1f685e05"),
"tags" : [
{
"Domain" : "http://www.google.com",
"rank" : 1
},
{
"Domain" : "https://www.stackoverflow.com/",
"rank" : 2
}
]
}
来推送然后弹出“找到的”字符串路径,而只需使用stack
和vector
字符串即可;然后您就可以按两种顺序打印出值!在这段代码中,我已从原来的顺序切换到“其他”顺序:
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;
}
循环:
for
答案 1 :(得分:0)
您可以简单地使用.rbegin(), .rend()
和reverse_iterator后退dictionary
,并在到达"cat"
时使用标志开始打印。参见std::vector,例如
#include <iostream>
#include <vector>
#include <string>
int main () {
bool prn = false;
std::vector<std::string> dictionary;
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");
/* output in reverse order beginning with cat */
for (auto it = dictionary.rbegin(); it != dictionary.rend(); it++) {
if (*it == "cat")
prn = true;
if (prn)
std::cout << *it << '\n';
}
}
使用/输出示例
如果我理解并且您只是想以"cat"
开始以相反的顺序打印,那么以下内容应与您尝试执行的操作相符:
$ ./bin/reverse_cats
cat
fog
cog
bog
dog
输出关联矢量索引
如果要与字符串一起输出矢量索引,则可以使用dictionary.rend() - it - 1
获取从零开始的索引,例如
/* output in reverse order beginning with cat */
for (auto it = dictionary.rbegin(); it != dictionary.rend(); it++) {
if (*it == "cat")
prn = true;
if (prn)
std::cout << dictionary.rend() - it - 1 << " " << *it << '\n';
}
使用/输出示例
$ ./bin/reverse_cats
4 cat
3 fog
2 cog
1 bog
0 dog