以递归方式将项添加到矢量

Add items to a vector recursively

本文关键字:添加 递归 方式      更新时间:2023-10-16

我正在尝试创建一个递归函数,该函数输出一个字符串向量,其中包含给定字符串的所有可能的单词组合(同时保留字母顺序)。基本上,这是自动更正打字程序的基础,该程序会产生类似于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);
            }
        }
    }
}