c++ - 在 C++ 中给定单词的字符串 vector 创建一个短语

标签 c++ vector letters phrase

到目前为止,我的 C++ 程序如下所示:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
    int i;
    int maxLet;
    const int NumWords = 26;
    const int NumPhrase = 8;
    string ele;
    string n[NumWords] = {
        "crypt",  "by",     "my",     "cysts",  "glyph", "psych", "shyly",
        "slyly",  "try",    "sylph",  "tryst",  "gypsy", "ply",   "pry",
        "shy",    "sly",    "spy",    "thy",    "cry",   "sty",   "glyphs",
        "sylphs", "trysts", "crypts", "psychs", "sprly"};
    vector<string> words(n, n + NumWords);
    vector<string> phrase(n, n + NumPhrase);
    for (i = 0; i < words.size(); i++) {
        cout << words.at(i) << endl;
        cout << words[i] << endl;
    }

    char aaaaa;
    cin >> aaaaa;
    return 0;
}

我需要创建一个类似于 [YYY] [YYYYY] [YYYBC] [GHLLLM] [PPPRR] [RS] [SS] [SSTTT] 其中字母全部打乱,括号表示单词的长度。 本质上,我需要用那些特定的词长和那些特定的字母数来创建一个词组 总共有, 11 岁 1 乙 1摄氏度 1克 1小时 3L的 1米 3个 3个 5秒的 3吨 我一直在努力想弄清楚该怎么做。非常感谢您的意见。

最佳答案

由于您所有的单词模板([...] 的集合)都是不同的,您只需计算每个此类元素的可能匹配项并将这些数字相乘即可。所以,让我们先写这样一个函数:

bool isMatch(string templ, string word)
{
sort(templ.begin(), templ.end());
sort(word.begin(), word.end());
return templ==word;
}

long long countMatches(string templ, vector<string> const& dict)
{
long long ret=0;
for(int i = 0; i < dict.size(); i++)
  ret += isMatch(templ, dict[i]);
return ret;
}

long long finalAnswer(vector<string> const& templs, vector<string> const& dict)
{
 long long ans=1;
 for(int i=0; i<templs.size(); i++)
   ans*=countMatches(templs[i], dict);
 return ans;
}

关于c++ - 在 C++ 中给定单词的字符串 vector 创建一个短语,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25098468/

相关文章:

c++ - 制作包含结构的 vector 的拷贝

C++删除指针

c++ - 如何在这个算法难题中应用背包方法?

c++ - 当你想减小 vector 的大小时,为什么 std::vector::resize() 不释放内存?

java - 如何在Java中存储 vector ?数据类型是什么?

c++ - 这个 GNU C,C++ 宏的扩展是什么

audio - 3D空间中声源位置的计算

python - 如何在Python中获取字母的所有排列以及字符串的大小写?

Javascript 将数组中的数字按每个两位数字序列求和

c# - 带瑞典字母的 OrderBy