枚举满足给定限制的所有字符串

发布于 2024-07-25 02:00:33 字数 238 浏览 2 评论 0原文

我正在寻找以下类别问题的名称,以便我可以通过谷歌搜索有效的算法和更多信息。

我有一个包含三个字符 {-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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

妄断弥空 2024-08-01 02:00:34

我不确定是否有一个明确定义的“算法类”; 这只是组合数学的练习。 您可以通过三个步骤进行生成:

  1. 生成设置了 8 位或更少位的所有 24 位数字(如果预先计算一些查找表,则可以稍微加快速度)
  2. 对于设置了 n 位的每个 24 位数字,迭代所有 n 位数字
  3. 如果 n 位数字的第 k 位为 0,则 24 位数字的第 k 位设置打印为 -1,否则打印为 1

更好地解释步骤 2-3假设您的 24 位数字设置了 4 位,如下所示

0 0 0 1 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 1 0 0 0 0

然后,我们迭代从 0 0 0 01 1 1 1 的所有 16 个 4 位数字,并且, 例如:

0 0 0 0 gives the string  0 0 0 -1 0 0 0 0 0 0 0 0 -1 -1 0 0 0 0 0 -1 0 0 0 0
0 1 1 0 gives the string  0 0 0 -1 0 0 0 0 0 0 0 0  1  1 0 0 0 0 0 -1 0 0 0 0
0 1 0 0 gives the string  0 0 0 -1 0 0 0 0 0 0 0 0  1 -1 0 0 0 0 0 -1 0 0 0 0
1 1 1 1 gives the string  0 0 0  1 0 0 0 0 0 0 0 0  1  1 0 0 0 0 0  1 0 0 0 0

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:

  1. Generate all 24-bit numbers with 8 or fewer bits set (you may be able to speed this up a bit if you precompute some lookup tables)
  2. For each 24-bit number with n bits set, iterate over all n-bit numbers
  3. If the kth bit of the n-bit number is 0, then the kth set bit of the 24-bit number prints as -1, otherwise it prints as 1

To explain steps 2-3 a bit better say your 24-bit number has 4 bits set and looks like

0 0 0 1 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 1 0 0 0 0

Then, we iterate over all 16 4-bit numbers from 0 0 0 0 to 1 1 1 1, and, for example:

0 0 0 0 gives the string  0 0 0 -1 0 0 0 0 0 0 0 0 -1 -1 0 0 0 0 0 -1 0 0 0 0
0 1 1 0 gives the string  0 0 0 -1 0 0 0 0 0 0 0 0  1  1 0 0 0 0 0 -1 0 0 0 0
0 1 0 0 gives the string  0 0 0 -1 0 0 0 0 0 0 0 0  1 -1 0 0 0 0 0 -1 0 0 0 0
1 1 1 1 gives the string  0 0 0  1 0 0 0 0 0 0 0 0  1  1 0 0 0 0 0  1 0 0 0 0
星光不落少年眉 2024-08-01 02:00:34

如果您只需要解决这个问题一次,也许您可​​以暴力破解它并将结果放入应用程序的查找表中。 需要检查的 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

淡笑忘祈一世凡恋 2024-08-01 02:00:34

与我的上一个答案完全不同,因为工作代码往往胜过研究论文的链接,我在 物理论坛,我自己不能把它归功于它,我只是修复了它,所以它在 g++ 下编译并更改为常量以在 24 中查找 8 位。它很快就枚举了所有 24 位字符串8 位,大约只有 735,000 个。 这些“模板”显示非零字符的唯一有效模式。 然后,您必须获取这 735,000 个答案中的每一个,并加上 -/+ 符号,并确定每个答案是否符合您的标准,但这样您就从 735,000 个可能的解决方案开始,而不是 2000 亿个。

#include <stdio.h>

 int main()
 {
 int size = 24;
 int pop = 8;

 int n = ((1 << pop) - 1) << (size - pop);

 while(true) {
    printf( "%x\n",n);

    int lowest = n & -n;

     if(lowest > 1) {
        n = n ^ lowest ^ (lowest >> 1);
        continue;
     }

     int high = n & (n + lowest);
     if(high == 0) break;

     int low = n ^ high;

     low = (low << 2) + 3;

     while((low & high) == 0) low <<= 1;
     n = high ^ low;
  }
 } 

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.

#include <stdio.h>

 int main()
 {
 int size = 24;
 int pop = 8;

 int n = ((1 << pop) - 1) << (size - pop);

 while(true) {
    printf( "%x\n",n);

    int lowest = n & -n;

     if(lowest > 1) {
        n = n ^ lowest ^ (lowest >> 1);
        continue;
     }

     int high = n & (n + lowest);
     if(high == 0) break;

     int low = n ^ high;

     low = (low << 2) + 3;

     while((low & high) == 0) low <<= 1;
     n = high ^ low;
  }
 } 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文