枚举满足给定限制的所有字符串
我正在寻找以下类别问题的名称,以便我可以通过谷歌搜索有效的算法和更多信息。
我有一个包含三个字符 {-1, 0, 1} 的字母表。
我需要有效地生成长度为 24 的所有字符串,其中大部分是 {0},但有零到八个 {1,-1} 字符以某些模式分布。 (这些模式涉及对 {-1} 的数量和配对的限制)。 符合我的标准的字符串总数相当有限:大约 128,000 个。
那么这类问题/算法的名称是什么?
I'm looking for the name of the following class of problem, so that I can google for effective algorithms and more information.
I have an alphabet with three characters {-1, 0, 1}.
I need to effectively generate all strings of length 24 which are mostly {0} but have zero to eight {1,-1} characters distributed in certain patterns. (The patterns involve restrictions on the number and pairings of {-1}). The total number strings that meet my criteria are quite modest: about 128,000.
So what is the name for this class of problem/algorithm?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不确定是否有一个明确定义的“算法类”; 这只是组合数学的练习。 您可以通过三个步骤进行生成:
更好地解释步骤 2-3假设您的 24 位数字设置了 4 位,如下所示
然后,我们迭代从
0 0 0 0
到1 1 1 1
的所有 16 个 4 位数字,并且, 例如:I'm not sure there's a well-defined "algorithm class" for this this; it's just an exercise in combinatorics. You can do the generation in three steps:
To explain steps 2-3 a bit better say your 24-bit number has 4 bits set and looks like
Then, we iterate over all 16 4-bit numbers from
0 0 0 0
to1 1 1 1
, and, for example:如果您只需要解决这个问题一次,也许您可以暴力破解它并将结果放入应用程序的查找表中。 需要检查的 0,1,-1 的 24 位序列不到一万亿。
如果我的数学计算错误或者您需要在运行时动态解决问题,我会将问题视为一个由 24 个变量组成的系统,每个变量限制为 -1, 0 ,1 并将其视为 约束满足问题,假设您可以以某种方式枚举您的约束。 然而,我担心的是,由于您需要查看所有解决方案而不仅仅是一个子集,因此您可能仍然陷入详尽地搜索问题空间的困境。
这篇论文似乎正合您的胃口:枚举约束满足问题的所有解决方案。 尽管我无法访问该论文的全文以查看它是否有帮助。
我可能完全找错了树,但也许这是一个起点
If you only need to solve this once, perhaps you could just brute force it and put the results in a lookup table in your application. There's less than a trillion 24 bit sequences of 0,1,-1 to check.
If perhaps I'm doing my math wrong or you need to dynamically solve the problem at run time, I would consider the problem as a system of 24 variables each limited to -1, 0 ,1 and approach it as a Constraint Satisfaction Problem, assuming you can enumerate your constraints in some way. My concern, however, is that since you require seeing all solutions and not just a subset, you may still be stuck exhaustively searching the problem space.
This paper seems right up your alley: Enumerating All Solutions for Constraint Satisfaction Problems. Though I don't have access to the full text of the paper to see if it helps.
I may be barking up the wrong tree all together, but perhaps this is a starting place
与我的上一个答案完全不同,因为工作代码往往胜过研究论文的链接,我在 物理论坛,我自己不能把它归功于它,我只是修复了它,所以它在 g++ 下编译并更改为常量以在 24 中查找 8 位。它很快就枚举了所有 24 位字符串8 位,大约只有 735,000 个。 这些“模板”显示非零字符的唯一有效模式。 然后,您必须获取这 735,000 个答案中的每一个,并加上 -/+ 符号,并确定每个答案是否符合您的标准,但这样您就从 735,000 个可能的解决方案开始,而不是 2000 亿个。
A completely seperate answer from my last, as working code tends to trump links to research papers, I found this code at Physics Forum and can not take credit for it myself, I just fixed it up so it compiled under g++ and changed to constants to look for 8 bits in 24. It very quickly enumerates all 24 bit strings with 8 bits on, and there are only about 735,000 of these. These 'templates' show the only valid patterns for your non-zero characters. You'd then have to take each of these 735,000 answers and throw around the -/+ signs and decide if each meets you criteria, but this way you are starting from 735k possible solutions instead of 200 Billion.