英文:
How to make combination sets of 15 words in go
问题
我有一组长度为15的单词,我想要列出所有可能的单词顺序。
第一个单词必须与第二个到第十五个单词组合,第二个单词必须与第一个和第三个到第十五个单词组合(除了第二个位置,第二个单词应该在15个索引中插入)。
很难准确解释我想要的内容,但我希望通过示例来帮助理解。
以4个数字为例(为了简单起见):
1 2 3 4
1 2 4 3
1 3 2 4
1 3 4 2
1 4 2 3
1 4 3 2
2 1 3 4
2 1 4 3
2 3 1 4
2 3 4 1
以此类推,以3、4开头的情况也是一样...
排列顺序并不重要,但是重要的是要生成所有长度为15的单词的可能性。
我的问题与算法有关,而不是具体的代码,感谢提供任何有用的算法或代码。
英文:
I have a sets of words with 15 length long, i want to make all possibilities words order.
First word have to came with all other words from 2th to 15th and with second word with 1th + 3th to 15th(second number should be imported in 15 indexes except of second index).
it's really hard to explain what i want exactly but i hope example help.
> Example with 4 number(for simplicity):
1 2 3 4
1 2 4 3
1 3 2 4
1 3 4 2
1 4 2 3
1 4 3 2
2 1 3 4
2 1 4 3
2 3 1 4
2 3 4 1
same with 3,4 at the first....
> ordering sets is not important at all but good, it's more important to make all the possibilities with all words with total length of 15.
My problem is with this algorithm not exactly with the code but, thanks for any helpful algorithm or code.
答案1
得分: 1
这是一个使用递归函数的解决方案。根据你的情况,你可以更改"elements"参数的类型。
从性能的角度来看,这可能不是最优的,但它应该能正确地工作。
package main
import "fmt"
func main() {
results := permutations([]int{1, 2, 3, 4, 5}, 3)
for _, perm := range results {
fmt.Println(perm)
}
fmt.Println(len(results)) // 应该打印出 5 * 4 * 3 = 60
}
func permutations(elements []int, length int) [][]int {
var results [][]int
var perm = make([]int, length) // 用于存储每个排列
used := make([]bool, len(elements)) // used[i] 表示第 i 个元素是否已经被使用
var dfs func(currentIndex, depth int)
dfs = func(currentIndex, depth int) {
perm[length-depth] = elements[currentIndex]
if depth == 1 {
result := make([]int, length)
copy(result, perm)
results = append(results, result)
return
}
used[currentIndex] = true
for i := range elements {
if used[i] {
continue
}
dfs(i, depth-1)
}
used[currentIndex] = false
}
// 从所有元素开始
for i := range elements {
dfs(i, length)
}
return results
}
英文:
Here is a solution with a recursive function. You can change the type of "elements" parameter according to your situation.
This might be suboptimal in terms of performance, but it should work correctly.
package main
import "fmt"
func main() {
results := permutations([]int{1, 2, 3, 4, 5}, 3)
for _, perm := range results {
fmt.Println(perm)
}
fmt.Println(len(results)) // should print 5 * 4 * 3 = 60
}
func permutations(elements []int, length int) [][]int {
var results [][]int
var perm = make([]int, length) // to write each permutation
used := make([]bool, len(elements)) // used[i] indicates whether i-th element is already used
var dfs func(currentIndex, depth int)
dfs = func(currentIndex, depth int) {
perm[length-depth] = elements[currentIndex]
if depth == 1 {
result := make([]int, length)
copy(result, perm)
results = append(results, result)
return
}
used[currentIndex] = true
for i := range elements {
if used[i] {
continue
}
dfs(i, depth-1)
}
used[currentIndex] = false
}
// start from all elements
for i := range elements {
dfs(i, length)
}
return results
}
答案2
得分: 0
你可以使用Heap算法:
https://wikipedia.org/wiki/Heap%27s_algorithm
伪代码在文章中有提供。
解决你的问题可能还有许多其他算法,但这是其中一种方法。
英文:
You can use Heap’s algorithm:
https://wikipedia.org/wiki/Heap%27s_algorithm
Pseudocode is in the article.
There are probably many other algorithms to solve your problem, but this is one way to do it.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论