next_permutation 用于幂集中的组合或子集
是否有一些等效的库或函数可以为我提供一组值的下一个组合,例如 dos 中的 next_permutation ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
是否有一些等效的库或函数可以为我提供一组值的下一个组合,例如 dos 中的 next_permutation ?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(6)
组合:从 Mark Nelson 关于同一主题的文章中,我们有 next_combination http://marknelson.us/ 2002/03/01/下一个排列
排列:从 STL 中我们有 std::next_permutation
Combinations: from Mark Nelson's article on the same topic we have next_combination http://marknelson.us/2002/03/01/next-permutation
Permutations: from STL we have std::next_permutation
我不知道其中之一。基本思想是将元素表示为位数组。例如,您有集合 S:
生成 S 的幂集(只需使用简单的加法生成大小 == 3 位的所有数字):
您所要做的就是找到设置了哪些位,并将它们与您的集合元素相关联。
最后一点,当您想要使用所有元素时,您可以生成一种组合,并且该组合就是集合本身,因为在组合中,顺序并不重要,所以我们肯定正在讨论许多元素 n,其中
0 <= n <= 大小(S)
I am not aware of one. The basic idea is to represent your elements as a bit array. So for example, you have the set S:
To generate the Power Set of S(just generate all numbers that are of size == 3 bits by using the simple addition):
All what you have to do is to find what bits are set, and to relate them to your set's elements.
On final note, there is one combination you can produce when you want all elements to be used and that combination is the set it self, because in combinations the order doesn't matter so for sure we are talking about a number of elements n where
0 <= n <= size(S)
在 Google 上搜索
C++“next_combination”
出现Googling for
C++ "next_combination"
turned up this.当我需要这样做时,我使用了这个库。它的界面与
std::next_permutation
非常相似,因此如果您以前使用过它,它会很容易使用。I've used this library when I've needed to do this. It has an interface very similar to
std::next_permutation
so it will be easy to use if you've used that before.幂集的枚举(即,所有大小的所有组合)可以使用二进制增量算法的改编。
不幸的是,双向迭代器的要求是可以解决的。
我打算让它处理相同的元素(多重集),但我需要去睡觉:v(。
用法:
编辑:这是多重集的版本。集合不必是 排序但相同的元素必须组合在一起。
已
Enumeration of the powerset (that is, all combinations of all sizes) can use an adaptation of the binary increment algorithm.
The requirement of a bidirectional iterator is unfortunate, and could be worked around.
I was going to make it handle identical elements (multisets), but I need to go to bed :v( .
Usage:
EDIT: Here is the version for multisets. The set doesn't have to be sorted but identical elements do have to be grouped together.
Output:
如果您别无选择,只能实现自己的功能,也许这种恐怖可以在该问题的答案中有所帮助,或者也许还有其他恐怖。
返回 k 元素的所有组合的算法来自 n
我前段时间写过它,现在我无法理解完整的图片:),但基本思想是这样的:
您拥有原始集合,当前组合是所选元素的迭代器向量。为了获得下一个组合,您从右到左扫描您的组合,寻找“气泡”。我所说的“气泡”是指未选择的一个或多个相邻元素。 “气泡”可能就在右侧。然后,在您的组合中,您将“气泡”左侧的第一个元素以及组合中右侧的所有其他元素与从“气泡”开头开始的相邻元素的子集交换气泡”。
In case You have no choice, but to implement Your own function maybe this horror can help a bit or maybe other horrors among answers to that question.
Algorithm to return all combinations of k elements from n
I wrote it some time ago and the full picture eludes me now :), but the basic idea is this:
You have the original set and current combination is a vector of iterators to the elements selected. To get the next combination, You scan your set from right to left looking for a "bubble". By "bubble" I mean one or more adjacent elements not selected. The "bubble" might be immediately at the right. Then, in Your combination, You exchange the first element at the left of the "bubble" and all other elements from the combination, that are to the right in the set, with a subset of adjacent elements that starts at the beginning of the "bubble".