Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/132.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 向量搜索押韵词_C++_Search_Vector - Fatal编程技术网

C++ 向量搜索押韵词

C++ 向量搜索押韵词,c++,search,vector,C++,Search,Vector,在我的程序中,我必须提示用户输入一个单词,并报告所有押韵的单词(通过检查最后3个字母是否相同) 例如,如果用户输入了单词“time”,我必须返回lime、dime、intime、regime等,形成一个有10.6万个单词的向量 所有10.6万个单词都在一个向量中vectorwords该向量将包含 time, lime, line, dime, intime, abaca, clilica, dog, ball, regime, sentence, return, which, contain,

在我的程序中,我必须提示用户输入一个单词,并报告所有押韵的单词(通过检查最后3个字母是否相同) 例如,如果用户输入了单词“time”,我必须返回lime、dime、intime、regime等,形成一个有10.6万个单词的向量

所有10.6万个单词都在一个向量中
vectorwords
该向量将包含

time, lime, line, dime, intime, abaca, clilica, dog, ball, regime, sentence, return, which, contain, word, pool, etc....
从所有这些中,我需要得到与用户输入的单词有节奏的单词


我如何创建一个函数,用用户输入的字符串来查找所有这些内容?

你说
押韵
=
最后3个字母是相同的
。矢量中的10.6万个单词意味着你有足够的内存,因此建议使用下面的方法以时间换取空间

unordered_map<string, vector<string>> rhymesMap;
int const rhymesSuffixLength = 3;

void preProcess(vector<string>& words){
    for(auto const& word: words){
        if(word.size() < rhymesSuffixLength)
            continue;
        string suffix = word.substr(word.size() - rhymesSuffixLength);
        rhymesMap[suffix].push_back(word);
    }
}

vector<string> getRhymes(string word){
    if(word.size() < rhymesSuffixLength)
        return {};
    string suffix = word.substr(word.size() - rhymesSuffixLength);
    return rhymesMap[suffix];
}
smap的无序映射;
int const lymmssuffixlength=3;
无效预处理(向量和单词){
for(自动常量和单词:单词){
if(word.size()
继续;
字符串后缀=word.substr(word.size();
押韵映射[后缀]。推回(单词);
}
}
向量getryms(字符串字){
if(word.size()
返回{};
字符串后缀=word.substr(word.size();
返回smap[后缀];
}

vector
搜索押韵太慢,
unordered\u map
将需要查找,而且速度相当快。

如果你处理的是英语单词,字母表是26个字符大。因此只有17576=263个存储桶。这意味着你可以使用具有连续内存的容器进行固定时间的查找

template <auto N, unsigned E>
inline constexpr auto power = N*power<N,E-1>;
template <auto N>
inline constexpr auto power<N,0> = decltype(N)(1);

template <unsigned suffix_len = 3, unsigned alphabet = 'z'-'a'+1>
class Rhyme {
  private:
    std::vector<std::vector<std::string>> table; // <----

    static unsigned serialise(std::string const& s) {
      unsigned result = 0;
      if (s.size() >= 3) {
        result += 1;
        for (auto it = std::next(std::begin(s),s.size()-3); it != std::end(s); ++it) {
          result *= alphabet;
          result += *it-'a';
        }
      }
      return result;
    }   
  public:
    Rhyme(std::vector<std::string> const& dictionary) : table{} {
      table.resize(power<alphabet,suffix_len>+1);
      for (auto const& s: dictionary) {
        if (auto index = serialise(s)) {
          table[index].emplace_back(s);
        }
      }
    }   

    std::vector<std::string> const& lookup(std::string const& key) const {
      return table[serialise(key)];
    }   
};

要创建一个函数,你需要编写它的代码,然后检查它是否编译并为你的输入生成正确的结果。似乎给你这个任务的人不知道什么是押韵。“intime”(法语发音)和“regime”(听起来像“team”)与“time”不押韵。但“time”与“time”是押韵的“押韵"。如果你有C++20,
std::string::ends_,首先对字符串最后三个字符的向量进行排序。请展示你的尝试。这是一个基本练习,给出解决方案会破坏你的练习。你知道如何获取字符串的最后三个字符吗?你知道如何比较两个字符串吗?我们不知道你在哪里stuck您应该通过引用传递这些函数参数。for循环还应该使用
auto&&
auto const&
。否则每次都会复制它们。谢谢!很快就忘了这一点。
std::vector<std::string> input =
  { "time", "lime", "line", "dime", "intime", "abaca", "clilica", "dog", "ball", "regime", "sentence", "return", "which", "contain", "word", "pool" };
Rhyme r(input);
for (auto const& s: r.lookup("slime")) {
  std::cout << s << "\n";
}
time
lime
dime
intime
regime