如何"iterate"这套
How to "iterate" over this set
你好,我有一些数据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
关键字。
我承认我也不完全理解这个问题。然而,解决这类问题的一般方法是想出一个好的符号和一个规范形式,以及一个将一些东西转换为规范形式的算法。然后你只需要做规范形式的重头戏,不要重复。
相关文章:
- 嵌套在类中时无法设置成员数据
- 无法访问嵌套类.类的使用无效
- 使用递归的数组的最小值.这是怎么回事
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- 如何在C++中初始化嵌套类中的2个memeber
- 如何声明特征矩阵,然后通过嵌套循环初始化它
- 当套接字连接断开时检测C/C++Unix
- 通过套接字[TCP]传输数据 如何在C / C ++中打包多个整数并使用send() recv()传输数据
- 在C++中搜索嵌套多映射值
- 在C++中将矢量转换为嵌套地图
- 我不小心调用了一个没有自己类对象的成员函数.但这是怎么回事呢
- 这是我尝试让用户将值输入到数组中.然后将其隐藏为大量的星号
- C++嵌套if语句,基本货币交换
- 如何通过套接字将文本文件的内容从服务器发送到客户端
- 在java中解决这段代码时面临循环中的问题
- 如何在C/C++中用FD_set Unix设置套接字文件描述符
- 嵌套的__try ..__除外,这是如何工作的
- C++ 嵌套'if'语句中的所有代码都会运行,即使这会更改外部语句的布尔值?
- 如何"iterate"这套