如何"iterate"这套

How to "iterate" over this set

本文关键字:这套 iterate 如何      更新时间:2023-10-16

你好,我有一些数据A -> .... (n个数据点)。现在我从这些数据中获取给定数量的值(m),现在我希望遍历这些值的所有唯一组合。

5个值的例子,你取2个唯一的:唯一的组合是"A + b"或"A + c",但"c + d"answers"b + c"是一样的。"B + E"等于"A + D"

A x x x x 
B x       x x
C   x     x 
D     x     x
E       x    

它们描述了一些几何"线",整个标本可以围绕中点"镜像"。因此,对于任意数量的行,是否存在一种聪明的算法来迭代考虑到"镜像能力"的所有内容?

在给定集合大小n和项目数量m的情况下,计算元素数量的公式是什么?

----一个带有"3 out 6"的例子:
它也非常类似于函数combine(6,3)——不过现在我用-而不是x来标记重复的行。

                    1 1 1 1 1 1 1 1 1 1 2
  1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
A x x x x x x x x - -                     A
B x x x x             x x - - - -         B
C x       x x x       x x -       - - -   C
D   x     x     x -   x     - -   - -   - D
E     x     x   x   -   x   -   - -   - - E
F       x     x   - -     -   - - - - - - F

所以可能的列表是:

ABC、ABD、ABE、ABF、ACD、ACE、ACF、ADE、BCD、BCE

20个候选人中有10个忽略了对称性

不容易。事实上这是相当困难的。下面是概要:

for_each_unmirrored() 
   mark first element as part of the set.
   mark last element as definitely NOT part of the set.
   //we know no matter whats inside, this isnt't semetrical, so all combinations
   for_each_mirrored() on all elements between these.
   mark first element as part of the set.
   mark last element as part of the set.
   //ends are semetrical, so insides cannot be
   for_each_unmirrored() on all elements between these
   mark first element as definitely not part of the set.
   mark last element as definitely not in the set.
   //ends are semetrical, so insides cannot be
   for_each_unmirrored() on all elements between these
for_each_mirrored() 
    for each element
        mark it as part of the set.
        if more elements are needed
            for_each_mirrored on elements to the right but in range

是的,即使是简化的伪代码也很复杂。真正的代码在这里:http://ideone.com/WDEn40(包括显示它适用于6个元素选择3)。链接的代码并不像它可能的那么简单,因为我优化了它以避免分配和最小化函数调用。(作为一个副作用,for_each_call_helper不是线程安全的。如果这需要线程安全,只需从该函数的变量中删除static关键字。

我承认我也不完全理解这个问题。然而,解决这类问题的一般方法是想出一个好的符号和一个规范形式,以及一个将一些东西转换为规范形式的算法。然后你只需要做规范形式的重头戏,不要重复。