生成组合如果物品只能组合一次
假设我有n个项目。 3),那么a不能与b nor C一起使用。
现在,我想创建m-item组合,但是如果已经存在组合{a,b,c}(在这种情况下为m = 1,2,3,4,5,6,7,8,9,10},m = 3,然后可能的组合应为:
- {1,2,3}
- {4,5,6}
- {7,8, 9}
- 的组合
- {10,1,2} - 这不是
- 有效 所有项目总是处于不同的组合中
- ...
我可以创建多少个组合?
我该如何系统地生成此类组合?
如果我允许两个项目可以组合两个不同的组合?,因此组合{1,2,3},{1,2,4}将有效。
Let's say I have N items. Now I want to create M-item combinations but if there already exist a combination {A,B,C} (in this case M=3), then A cannot be in another combination together with B nor C.
Example: N={1,2,3,4,5,6,7,8,9,10}, M=3, then possible combinations should be:
- {1,2,3}
- {4,5,6}
- {7,8,9}
- {10,1,2} - this is not a valid combination because 1 and 2 are already in a generated combination {1,2,3}
- {10,1,4}
- {2,5,8} - valid because all items were always in different combinations
- ...
How many combinations I can create?
How could I systematically generate such combinations?
What if I allow that two items can be together in two different combinations? So combinations {1,2,3} and {1,2,4} would be valid.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
蛮力,但很快。在
python
中,但我相信:Brute force, but pretty quick. In
python
, but clear on approach, I believe:我想您可以使用标准组合生成算法,只需添加二手对的验证(在Java中)(在Java,但很清楚如何移植到任何其他语言):
对于3-10,它会生成10个组合,我想它是最大数字在子集中,请随时证明我错了
它应该很容易扩展以支撑唯一的三重态,而不是对,只需生成字符串键,例如comb 设置为
min(a,b,c) +“ _” +“ _” + middle(a,a, b,c) +“ _” + max(a,b,c)
而不是位于使用的
使用设置I suppose you can use standard combination generation algorithm, just add validation for used pairs, like this (in java, but pretty clear how to port to any other language):
for 3-10 it generates 10 combinations and I suppose it is maximal number of subsets, feel free to prove me wrong
it should be moderately easy to extend it to support unique triplets instead of pairs, just generate string key like
min(a,b,c) + "_" + middle(a,b,c) + "_" + max(a,b,c)
instead of bitwise hack I used to speed up and add all fromcomb
set intoused
set