到目前为止,我的 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/