求字母表{a,b,c}上长度为n的字符串w的个数
Find the number of strings w of length n over the alphabet {a, b, c}
我想知道如何计算所有长度为n的字符串的数量,使字符串w的任何长度为4的子字符串,所有三个字母a、b、c都出现。例如,当n=9时,应打印abbcaabca,但不应包括aabbcabac。
我试着做一个类似的数学公式
3^N - 3 * 2^N + 3 or (3^(N-3))*N!
它能这样工作吗?还是我必须生成它们并计数?我正在处理像100这样的大数字,我认为我无法生成它们来计算它们。
您可能应该能够逐步提高,从让我们说出所有长度为4的可能单词开始,然后只添加一个字母,并计算可能允许的结果单词。然后,你可以迭代地达到高数字,而不必探索所有3^N的可能性。
const unsigned w = 4;
unsigned n = 10;
vector<string> before,current;
// obtain all possible permutations of the strings "aabc", "abbc" and "abcc"
string base = "aabc";
before.emplace_back(base);
while(std::next_permutation(base.begin(),base.end())) before.emplace_back(base);
base = "abbc";
before.emplace_back(base);
while(std::next_permutation(base.begin(),base.end())) before.emplace_back(base);
base = "abcc";
before.emplace_back(base);
while(std::next_permutation(base.begin(),base.end())) before.emplace_back(base);
// iteratively add single letters to the words in the collection and add if it is a valid word
size_t posa,posb,posc;
for (unsigned k=1;k<n-w;++k)
{
current.clear();
for (const auto& it : before)
{
posa = it.find("a",k);
posb = it.find("b",k);
posc = it.find("c",k);
if (posb!= string::npos && posc!= string::npos) current.emplace_back(it+"a");
if (posa!= string::npos && posc!= string::npos) current.emplace_back(it+"b");
if (posa!= string::npos && posb!= string::npos) current.emplace_back(it+"c");
}
before = current;
}
for (const auto& it : current) cout<<it<<endl;
cout<<current.size()<<" valid words of length "<<n<<endl;
注意,有了这个,你仍然会很快遇到指数墙。。。在一个更有效的实现中,我会将单词表示为整数(不是整数的向量,而是基数为3的整数表示),但指数缩放仍然存在。如果你只是对数字感兴趣,@Jeffrey的方法肯定会更好。
诀窍是分解问题。考虑:
知道有多少这样的字符串,长度为50,以每对字母结尾,会有帮助吗?
50个字符串的数目,以AA次结束50个字符串的数目,以B或C开头+50个字符串的数目,以AB次结束50字符串的编号,以C开头+所有其他组合都提供了100个长字符串的数量。
继续递归地分解它。
查找动态编程。
还可以查找大量的库。
相关文章:
- 如何使用Luacneneneba API正确读取字符串和表参数
- 如何在C++中移动字母表中的字母?
- 循环回到z之后的字母表的开头
- 关于在C++中访问字符串字母
- 为什么我会在试图计算字符串字母的程序中遇到此错误
- C 分类字符串按大小和字母表
- 错误:“命令” /重复使用ASM挥发性之前的预期字符串字母
- 我在使用布尔函数时遇到问题,其中用户输入两个字母并输出字母表中最高的字母
- 使用列表向后打印字母表时缺少'a'
- 警告:数组"字母表"由括号字符串文本初始化
- 如何检查字符是否是某个字母表中的字母
- 为什么在此 C 代码中将字母表拆分为多个范围?
- c++如何确定字母表中一个单词是否在另一个单词之前
- 计算C++中每个字母表的出现次数
- 求字母表{a,b,c}上长度为n的字符串w的个数
- 如何检查字母表中的字母在文本文件中出现的次数C++
- 将莫尔斯值解码为字母表
- 在 C 语言中,求解从字母表中输入的整数并重复循环
- 按字母表对字符串数组进行排序并显示值
- 如何在c++中检查字符串是否包含字母表中的每个字母