C++字符串向量
我正在尝试从字符串中创建一个数据树,这些字符串至少扩展1个字母,并且可以从当前起始字访问。在这个例子中,我的起始词是狗,结尾词是猫。我必须检查字典中的单词大小是否相同,是否已经出现在矢量词中,如果只有1个字母的差异。 下面我已经尝试过实现这种想法,但我认为我遗漏了一些关键的东西。我需要帮助寻找或添加到我的代码中C++字符串向量,c++,vector,C++,Vector,我正在尝试从字符串中创建一个数据树,这些字符串至少扩展1个字母,并且可以从当前起始字访问。在这个例子中,我的起始词是狗,结尾词是猫。我必须检查字典中的单词大小是否相同,是否已经出现在矢量词中,如果只有1个字母的差异。 下面我已经尝试过实现这种想法,但我认为我遗漏了一些关键的东西。我需要帮助寻找或添加到我的代码中 #include <iostream> #include <bits/stdc++.h> #include <cctype> // For
#include <iostream>
#include <bits/stdc++.h>
#include <cctype> // For the letter checking functions
#include <fstream> // For file input
#include <vector> // For the use of vectors
#include <cstdlib> // For exit and abs
using namespace std;
int main(){
vector<string> dictionary;
vector<string> words;
string startWord = "dog";
string endWord = "cat";
dictionary.push_back("dog");
dictionary.push_back("bog");
dictionary.push_back("cog");
dictionary.push_back("fog");
dictionary.push_back("cat");
words.push_back(startWord);
int counter = 0;
int countline = 0;
while( counter < words.size() ){
cout << words[counter] << ": ";
for(int j = 0; j < dictionary.size(); j++){
for(int l = 0; l < startWord.size(); l++){
for(int k = 0; k < dictionary.at(j).length(); k++){
if(dictionary.at(j)[k] == startWord[k]){
counter++;
if(counter == 1){
countline++;
words.push_back(dictionary[l]);
cout << words[l]<< endl;
cout << " The succeded word is "<< words[l] << endl;
}
}
}
}
}
} // ends while
为了避免无限循环,您需要记住您已经看到的单词。在下面的代码示例中,我为该add include使用了一个无序的_集 然后,代码可以如下所示:
#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;
}
可以找到6个嵌套控制结构的实时版本,其中4个是循环。是的,我们陷入了困境。我不明白你想做什么。至少扩展1个字母是什么意思。你说的是树,但我只看到向量。我不知道你的例子应该说明什么。例如,显示完整的输入和输出。很抱歉,该措辞已关闭。所以我要创建的程序是一个将一个单词转换为另一个单词的程序。例如,我想把dog这个词改成cat。然后,我想显示的话,可以扩大从狗只切换1个字母。所以从狗身上我可以得到齿轮,沼泽,雾,齿轮,慢跑…等等。所有这些单词都是相同长度的真实单词,并且只改变一次@你有一本真词词典。开始词和结束词。您从startWord开始,通过成功的转换,希望结束于endWord。从一个单词到另一个单词的成功转换是只更改一个字母,结果单词必须在字典中。我理解正确吗?你需要重新思考你的方法。随着数据集大小的增加,此代码的性能将大幅降低。研究哈希表、集合的使用,避免嵌套循环。由于您只对具有单个差异的单词感兴趣,因此只要有两个差异,就无需继续计数字符。对于较长的单词,这可以节省很多时间。如果我想检查新词是否等于结束词,我可以在你离开的那一个之后再添加一个if循环吗?if语句不是循环。但是,是的。只需添加ifcandidate==endwordcout,就可以根据索引显示从一个单词到另一个单词的顺序。例如,如果dog是索引0的起始词。狗,我得到了cog,它在索引2,以此类推,直到我得到cat?这似乎是一个复制/粘贴的问题,你以前有一个词的定义。我添加了一个链接到一个在线编译器,在那里你可以看到结果。
#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 dog: (1)bog (2)cog (3)fog
1 bog: (4)bag (5)beg
2 cog:
3 fog:
4 bag: (6)bat
5 beg: (7)bet
6 bat: (8)cat Found endword
Word path:
dog bog bag bat cat