如何“迭代”这个集合

时间:2013-06-04 23:28:30

标签: c++ combinations

你好我说有一些数据A -> ....(n个数据点)。现在我从这个数据中得到一定数量的值(m) - 现在我希望迭代这些值的所有唯一组合。

5个值的示例,其中您将2个唯一值: 独特的组合将是类似“a + b”或“a + c”的东西 - 但是“c + d”与“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出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人无视对称性。

2 个答案:

答案 0 :(得分:1)

不容易。事实上,这很困难。但这是破败的:

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关键字。)

答案 1 :(得分:0)

我承认我也不完全理解这个问题。但是,解决这类问题的一般方法是提出一个好的符号和规范形式,以及将某些东西转换为规范形式的算法。那么你只需要在规范形式上做重点,不要重复。

相关问题