递归地将项目添加到向量中
我正在尝试创建一个递归函数,该函数输出一个字符串向量,其中包含给定字符串的所有可能的单词组合(同时保留字母顺序)。基本上,这是自动更正打字程序的基础,可产生与 iPhone 类似的效果。
vector<string> allPossibleWords(string str, vector<vector<char> > & adjacentKeys)
{
vector<string> words;
cout << str << endl;
if (str.length() == 0)
{
return words;
}
char firstLetter = str[0];
string restOf = str.substr(1, str.length() - 1);
int position = position_in_vector(firstLetter);
for (int i = 0; i < adjacentKeys[position].size(); i++)
{
string temp(1, adjacentKeys[position][i]);
words.push_back(temp);
}
//allPossibleWords(restOf, adjacentKeys);
}
int position_in_vector(char letter)
{
return (letter % 97);
}
例如,如果 str 是“yp”,则输出应该是包含值 {“yp”、“tp”、“gp”、“hp”、“up”、“yo”、“to”、“go”的向量”、“ho”、“uo”、“yl”、“tl”、“gl”、“hl”、“ul”}。如果 str 为“y”,则输出应为包含值 {“y”、“t”、“g”、“h”、“u”} 的向量。
存储在相邻键中的 26 个向量包含与存储在向量的第一个位置的字母相邻的字母。
a qwsz
b vghjn
c xdfgv
d zserfcx
//and so on
我被这个函数困住了,不知道如何递归地构建这个向量。
I'm attempting to create a recursive function that outputs a vector of strings that contains all possible word combinations (while retaining order of letters) of a given string. Basically, the foundation of an auto-correct typing program, which produces effects similar that of the iPhone.
vector<string> allPossibleWords(string str, vector<vector<char> > & adjacentKeys)
{
vector<string> words;
cout << str << endl;
if (str.length() == 0)
{
return words;
}
char firstLetter = str[0];
string restOf = str.substr(1, str.length() - 1);
int position = position_in_vector(firstLetter);
for (int i = 0; i < adjacentKeys[position].size(); i++)
{
string temp(1, adjacentKeys[position][i]);
words.push_back(temp);
}
//allPossibleWords(restOf, adjacentKeys);
}
int position_in_vector(char letter)
{
return (letter % 97);
}
For instance, if str is "yp", the output should be a vector containing the values {"yp", "tp", "gp", "hp", "up", "yo", "to", "go", "ho", "uo", "yl", "tl", "gl", "hl", "ul"}. If str is "y", the output should be a vector containing the values {"y", "t", "g", "h", "u"}.
The 26 vectors stored in adjacentKeys contain the letters adjacent to the letter that is stored in the first position of the vector.
a qwsz
b vghjn
c xdfgv
d zserfcx
//and so on
I am stuck with this function, and can't figure out how to recursively build this vector.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
(更新:2130 GMT 周日:我已经显着改变了我的答案。我认为这现在有效。)
这是一个完整的程序。我想我还会做出其他改变,但我会努力保持您最初解决方案的精神。当
str.length()==0
时返回一个空单词很重要。返回
(Update: 2130 GMT Sunday: I've significantly changed my answer. I think this works now.)
Here is a complete program. There are other changes I think I would make, but I'm trying to keep to the spirit of your initial solution. It's important to return a single empty word when
str.length()==0
.return
也许是这样的?我还没有测试它,因为我没有你的
adjacentKeys
矩阵。它可能可以进行一些优化,但我认为这种方法根本无法很好地扩展。我建议从不同的角度解决这个问题,也许将你的字典存储在某种 K-ary 中树,并有几个指针在树上行走,根据邻接矩阵跟随分支。这将阻止无效单词的生成(以及随后的检查有效性的查找),因为分支仅存在于有效单词存在的地方。
Maybe something like this? I haven't tested it because I don't have your
adjacentKeys
matrix. It can probably be optimised a bit, but I don't think this approach will scale well at all.I'd suggest attacking the problem from a different angle, perhaps storing your dictionary in some kind of K-ary tree, and having several pointers walking the tree, following branches based on your adjacency matrix. This would stop the generation of invalid words (and subsequent lookups to check validity) as branches would only exist where valid words exist.