如何从多棵树中获取所有组合?
除了使用 x 嵌套 for 循环之外,我不知道如何从 x 组数据中获取所有可能性,我不想这样做,因为我不知道 x 的值,这使得 for 循环的数量变得非常可能硬编码不等于所需的循环数。
假设我有:
A:1,2,3,4
B:2,4,65,2
C:1,3,2
D:2,8
并且我想从每个组中获取 1 个项目的所有组合(在我的代码中,我使用 std::vector
),我该怎么做?有人可以发布一个我可以遵循的通用伪代码来执行此操作吗?
I have no idea how to get all the possibilities from x sets of data other than using x nested for loops, which i dont want to do, because i dont know the value of x, which makes it very possible for the number of for loops hard coded not equal to the number of loops needed.
say i have:
A:1,2,3,4
B:2,4,65,2
C:1,3,2
D:2,8
and i wanted to get every combination of 1 item from each group (in my code, im using std::vector <myclass>
), how would i do that? can someone please post a general pseudocode i can follow to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您不知道自己有多少个“组”,我假设您至少拥有所有这些组的一些集合?即,所有向量的数组/向量?
如果是这样,这里有一个大概的想法,
对正在发生的事情的基本解释:
当您调用该函数时,它将开始遍历“顶部”组中的每一项(组数组中的最后一项)。如果除了“顶部”组之外还有更多组,它将调用相同的函数,传递到目前为止的当前项目,并告诉它查看下一组。
下一个级别将开始检查每个项目,并以相同的方式运行。这个过程一直持续到你到达底部组,在那里,它不会调用另一个级别,而是将所有结果的集合传递给你的回调函数,回调函数将使用它们。
当底层遍历完所有项目后,它将返回到上一层,仍处于“foreach”循环中。上一层会转到下一项,并调用底层,从头开始。这样一直持续下去,直到最终“最高”级别也完成了每个项目。
这是伪代码,具体内容根据您的语言而变化。
“临时结果”可以是声明和分配的预先数组(就像我在伪代码中所示),也可以是在每次递归调用之前压入并在之后弹出的堆栈,也可以是数组/您在每次递归调用之前复制并附加的向量。您甚至可以有一个链表,每个节点都存储在该函数调用的堆栈帧中,并且您只需将“父级”的指针传递到下一级。
如果您想要做一件特定的事情,您可以将“callback_function”替换为该特定的事情。或者,如果您的语言允许,您可以使用它作为迭代器/生成器,并让“回调”是隐式的。
如果您真的讨厌递归代码,您可以编写与 while 循环相同的东西,但它更直接将其写为递归函数。
if you don't know how many 'groups' you have, I assume you have at least some collection of all of them? i.e., an array/vector of all your vectors?
if so, here's a general idea of what would work
A basic explanation of what's going on:
when you call the function, it will start going through each item on the 'top' group (the last one in the array_of_groups). If there's more groups beyond the 'top' one, it will call the same function, passing the current items so far, and telling it to look at the next group down.
This next level down will start going through each item, and behave the same way. This continues until you get to the bottom group, where, instead of calling another level down, it will pass the collection of all results to your callback function, which will use them.
When the bottom level has gone through all items, it will return to the next level up, which is still in its 'for each' loop. the next level up will go to the next item, and call the bottom level, which will start from the beginning. this continues until eventually even the 'top' level has gone through each item.
That's the pseudocode, the specifics change depending on your language.
the 'temp result' can be a declared and allocated up-front array, (like I show in the pseudocode), or it can be a stack that gets pushed before each recursive call, and popped after, or it can be an array/vector that you copy and append before each recursive call. You could even have a linked list, with each node just stored in the stack frame for that function call, and you'd just have to pass the pointer of the 'parent' to the next level down.
If you have one specific thing you want to do, you can replace the 'callback_function' with that specific thing. Or, if your language allows it, you can use this as an iterator/generator, and let that ' callback' be implicit
If you really hate recursive code, you can write the same thing as a while loop, but it's much more straightforward to write it as a recursive function.