以递归方式将项添加到矢量
Add items to a vector recursively
我正在尝试创建一个递归函数,该函数输出一个字符串向量,其中包含给定字符串的所有可能的单词组合(同时保留字母顺序)。基本上,这是自动更正打字程序的基础,该程序会产生类似于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
我被这个函数困住了,无法弄清楚如何递归构建这个向量。
(更新:格林威治标准时间周日2130:我显着改变了我的答案。我认为这现在有效。
这是一个完整的程序。我认为我会进行其他更改,但我正在努力保持您最初解决方案的精神。str.length()==0
时返回一个空词很重要。
#include <vector>
#include <iostream>
using namespace std;
vector<string> allPossibleWords(string str, vector<vector<char> > & adjacentKeys)
{
vector<string> words;
// cout << "str=" << str << endl;
if (str.length() == 0)
{
words.push_back("");
return words;
}
char firstLetter = str[0];
// cout << "firstLetter=" << firstLetter << endl;
int positionInAdjacentKeys = 0;
while(positionInAdjacentKeys < adjacentKeys.size() && adjacentKeys.at(positionInAdjacentKeys).front() != firstLetter) {
++ positionInAdjacentKeys;
}
vector<char> & adjacent = adjacentKeys.at(positionInAdjacentKeys);
string restOf = str.substr(1, str.length() - 1);
// cout << firstLetter << ":" << restOf << endl;
// int position = position_in_vector(firstLetter);
vector<string> recursiveWords = allPossibleWords(restOf, adjacentKeys);
for (int i = 0; i < adjacent.size(); i++)
{
const string temp(1, adjacent[i]);
// cout << " temp=" << temp << endl;
for(vector<string>::const_iterator i = recursiveWords.begin(); i != recursiveWords.end(); i++)
{
// cout << "new word=" << temp + *i << endl;
words.push_back(temp + *i);
}
}
return words;
}
int main() {
vector<vector<char> > adj;
vector<char> v1;
v1.clear();
v1.push_back('p');
v1.push_back('o');
v1.push_back('l');
adj.push_back(v1);
v1.clear();
v1.push_back('y');
v1.push_back('t');
v1.push_back('g');
v1.push_back('h');
v1.push_back('u');
adj.push_back(v1);
adj.push_back(v1);
vector<string> words = allPossibleWords("yp", adj);
for(vector<string> :: const_iterator i = words.begin(); i != words.end(); i++) {
cout << *i << endl;
}
}
返回
也许是这样的东西? 我还没有测试过它,因为我没有你的adjacentKeys
矩阵。 它可能可以稍微优化一下,但我认为这种方法根本无法很好地扩展。
我建议从不同的角度解决这个问题,也许将字典存储在某种 K 元树中,并在树上放置几个指针,根据邻接矩阵跟踪分支。 这将停止生成无效单词(以及随后的查找以检查有效性),因为分支仅在存在有效单词的情况下存在。
using namespace std;
void allPossibleWordsHelper(const string& str,
string::size_type index,
const vector<vector<char> >& adjacentKeys,
vector<string>& results)
{
if (str.length() == 0)
{
return;
}
std::string head = (index > 0) ? str.substr(0, index) : "";
std::string tail = (index < str.length() - 1) ? str.substr(index + 1) : "";
vector<string> possibleHeads;
string::size_type headIndex = (str.length() - index) / 2;
allPossibleWordsHelper(head, headIndex, adjacentKeys, possibleHeads);
vector<string> possibleTails;
allPossibleWordsHelper(tail, index + headIndex, adjacentKeys, possibleTails);
int pos = str[index] - 'a';
vector<string>::const_iterator headi;
vector<string>::const_iterator headi_end = possibleHeads.end();
vector<string>::const_iterator taili;
vector<string>::const_iterator taili_end = possibleTails.end();
vector<char>::const_iterator aki;
vector<char>::const_iterator aki_end = adjacentKeys[pos].end();
for(headi = possibleHeads.begin(); headi != headi_end; ++headi)
{
for (aki = adjacentKeys[pos].begin(); aki != aki_end; ++aki)
{
for (taili = possibleTails.begin(); taili != taili_end; ++taili)
{
string suggestedWord = *headi + *aki + *taili;
results.push_back(suggestedWord);
}
}
}
}
相关文章:
- 通过递归进行因子分解
- 递归函数计算序列中的平方和(并输出过程)
- 递归添加子树以提升属性树
- 编写递归算法以从链表中删除元素.编写递归算法以将元素添加到链表中
- 如何在Qt中递归添加源文件
- 递归地将 1 添加到 BST 中的所有节点,但具有最小数据的节点除外
- 简单的二进制搜索树非递归添加功能
- c++ 递归添加,4*x
- 如何使用递归向字符串添加逗号
- 递归函数,添加传递给它的两个变量
- 将目录添加到Xcode自定义项目模板会产生奇怪的递归
- 使用递归将元素添加到向量中
- 递归添加到带有点 c++ 的向量
- 用内部类函数递归地添加数组数
- 递归-按顺序向列表中添加树数据
- 将基数10转换为12,在递归地添加字母字符时遇到麻烦
- 递归函数扫描字符串以添加ASCII
- 以递归方式将项添加到矢量
- QTDom -递归地向特定元素添加子元素
- 为什么添加相同的东西会翻转递归