【问题标题】:How to "iterate" over this set如何“迭代”这个集合
【发布时间】:2013-06-04 23:28:29
【问题描述】:

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

取 2 个唯一值的 5 个值的示例: 一个独特的组合将是“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 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 个忽略了对称性。

【问题讨论】:

  • 为什么B+E和A+D一样?
  • @OliCharlesworth 当您在中间翻转 B + E 时(在此示例中为“C”,偶数将位于两行之间)。 B 转换为 D,E 转换为 A。
  • 哦,我明白了。那么在这种情况下,绘制一个 NxN 网格,并在与您要生成的组合相对应的所有坐标中放置一个 X。此时模式应该很明显。
  • 我无法弄清楚显示的网格如何与描述的任何部分匹配。这个问题对我来说几乎没有意义
  • 样本是这样处理一组五个元素中的两个元素的吗? ideone.com/No3iA2

标签: c++ combinations


【解决方案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 关键字。)

【讨论】:

    【解决方案2】:

    我承认我也没有完全理解这个问题。但是,解决此类问题的一般方法是提出一个好的符号和规范形式,以及将某些内容转换为规范形式的算法。然后,您只需在规范形式上做繁重的工作,不要重复。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-12
      • 1970-01-01
      • 2018-01-26
      • 1970-01-01
      • 2015-08-23
      • 2022-01-02
      • 1970-01-01
      相关资源
      最近更新 更多