查找给定数字集的所有组合
假设我有一组数字“0”、“1”、“2”、...、“9”。我想找到恰好包含我的集合中每个数字之一的所有数字。
问题是:在开始我的程序之前,我不知道我的集合将包含多少个数字以及哪些数字。 (例如,该集合可能包括数字“1”、“3”和“14”。)
我在互联网上搜索,偶然发现了术语“动态编程”,它显然可以用来解决像我这样的问题,但是我不明白这些例子。
有人可以给我一个关于如何解决这个问题的提示(可能使用动态编程)吗?
编辑:当该集合包含像“14”这样的数字时,该集合的不同数字当然必须通过某种方式分隔,例如,当该集合包含数字“1”、“3”和“14”时,组合可以类似于 1-3-14 或 3-14-1(= 由“-”字符分隔的各个数字)。
编辑2:此处描述了一个似乎有些相似的问题:其中一种解决方案使用动态规划。
say I have a set of numbers '0', '1', '2', ..., '9'. I want to find all numbers that contain exactly one of each of the numbers in my set.
The problem is: Before I start my program, I do not know how many numbers and which numbers my set will include. (For example, the set could include the numbers '1', '3' and '14'.)
I searched the internet, and stumbled upon the term 'dynamic programming' which apparently is something to use to solve problems like mine, but I did not understand the examples.
Can somebody give me a hint on how to solve this problem (possibly with dynamic programming)?
EDIT: When the set includes numbers like '14' the different numbers of the set would of course have to be separated by some means, e.g. when the set includes the numbers '1', '3', and '14', combinations could be something like 1-3-14 or 3-14-1 (= individual numbers separated by a '-'-character).
EDIT 2: One problem that seems to be somewhat similar is described here: one of the solutions uses dynamic programming.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
对我来说,看起来您正在寻找给定元素集的所有排列。
如果您使用 C++,则有一个标准函数
next_permutation()
可以完全满足您的需求。您从排序后的数组开始,然后重复调用next_permutation
。示例如下: http://www.cplusplus.com/reference/algorithm/next_permutation/
To me, it looks like you are looking for all permutations of a given set of elements.
If you use C++ there is a standard function
next_permutation()
that does exactly what you are looking for. You start with the sorted array and then callnext_permutation
repeatedly.The example is here: http://www.cplusplus.com/reference/algorithm/next_permutation/
为了在事先不知道必须有多少位输出的情况下检查所有组合,我曾经编写过这段代码:
它只在一个周期内完成所有操作,以避免递归和函数调用开销,仍然使用“伪造”所需的嵌套 for 循环堆栈数组。
表现相当不错,在我的 4 年前的 Athlon64 3800+ 上,需要 2' 4" 的用户时间(=> 实际计算时间)来生成 36^6=2176782336 个组合,因此它每秒计算大约 1750 万个组合。
它的 “实时”时间相当长,因为我同时还在电脑上做其他事情。
To examine all the combinations without knowing in advance how many digits must have the output, I once wrote this code:
It does everything just in one cycle to avoid recursion and function calls overhead, still if "fakes" the needed nested for loops using the stack array.
It performs quite well, on my 4 years old Athlon64 3800+ it takes 2' 4" of user time (=> actual computation time) to generate 36^6=2176782336 combinations, so it computes about 17.5 million combinations per second.
The "real" time is quite high because I was also doing something else on the PC in the meanwhile.
您正在寻找给定值集的所有排列。
一篇关于用 Java 进行排列的文章如下: http://www.bearcave.com/ random_hacks/permute.html
您想要跳过前几个部分,直到到达标题排列算法(当然)。
You're looking to find all permutations of a given set of values.
One article on "doing" permutations in Java is here: http://www.bearcave.com/random_hacks/permute.html
You want to skip the first couple of sections until you get to the heading Permutation algorithms (of course).
这是我的排列的 C# 3.0 实现,您会发现有用
Here is my C# 3.0 implementation of permutations you can find useful
有多少数字和哪些数字不是两个问题。如果你知道哪些数字,你就知道有多少。
而且数字的名称也不是很有趣。 1-3-14 或 0-1-2 或 Foo-Bar-Baz - 总是同样的问题,与 0-1-2 的排列和数组相同的问题,在哪里查找结果。
最方便的解决方案是编写一个通用的 Iterable。然后您可以使用简化的 for 循环来访问每个排列。
How many numbers, and which ones, aren't two questions. If you know which numbers, you know how many.
And the names of the numbers aren't very interesting. 1-3-14 or 0-1-2 or Foo-Bar-Baz - it is always the same problem, the same problem as the permutations of 0-1-2 and with an array, where to look up the result.
The most convenient solution is, to write a generic Iterable. Then you can use the simplified for-loop, to access each permutation.
与动态规划无关;除非你想把内裤穿在裤子外面,并在胸前画一个符号。
简单的方法是维护一个 0-9 整数数组,然后逐一遍历数字并递增 array[num]。处理完所有数字后,结果是查看数组中是否有任何元素非零或一。 (这表示重复的数字。)当然,获取一个数字,然后使用模数和除数逐位迭代是很简单的。
Nothing to do with dynamic programming; unless you want to wear underpants outside your trousers and paint a symbol on your chest.
Simple way to do it is maintain an array of 0-9 of integers, then run through the numbers one by one and increment array[num]. The result, once you've processed all digits, is to see if any element of the array is non-zero or one. (That indicates a repeated digit.) Of course, it's trivial to take a number and then iterate through digit by digit using modulus and divisor.
因此,假设您有数字 1、2 和 3。
如果您期望 123、132、213、231、312 和 321 这六个数字是正确答案,那么您需要的是一些代码来生成所有数字对于大小有趣的问题,这几乎比任何其他方法都要快。不过,您将 O(n!) 视为最好的情况。
So, let's say you have the numbers 1, 2 and 3.
If you are expecting the six numbers 123, 132, 213, 231, 312 and 321 to be the correct answer, what you're looking for is some code to generate all permutations of a set, that'll be faster than almost anything else for problems of an interesting size. You're looking at O(n!) as a best case, though.
您应该编写一个递归函数,循环遍历列表,并且每次都使用更新的列表调用自身。这意味着它需要创建包含 N-1 个元素的列表副本以传递到下一次迭代。对于结果,您需要在每次迭代中附加当前选定的数字。
You should write a recursive function that loops through the list and every time calls itself with an updated list. This means it needs to create a copy of the list with N-1 elements to pass to the next iteration. For results, you need to append the currently selected number in each iteration.