-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcombine.go
More file actions
200 lines (176 loc) · 3.5 KB
/
Copy pathcombine.go
File metadata and controls
200 lines (176 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package algorithm
import (
"container/list"
"fmt"
)
var LogEnable = false
func log(a ...interface{}) {
if LogEnable {
fmt.Println(a...)
}
}
func CombNumber(n, m uint64) uint64 {
a := uint64(1)
b := uint64(1)
for i := m; i > 0; i-- {
a *= n
n -= 1
b *= i
}
return a / b
}
func CombinerSelect(result *list.List, data, workspace []byte, m, n int) {
if len(workspace) == n {
result.PushBack(workspace)
//log(workspace)
return
}
for i := 0; i < len(data); i++ {
copyWorkspace := make([]byte, len(workspace))
copy(copyWorkspace, workspace)
copyWorkspace = append(copyWorkspace, data[i])
copyData := data[i+1:]
if len(copyData)+len(copyWorkspace) < n {
continue
}
CombinerSelect(result, copyData, copyWorkspace, m, n)
}
}
type CombineTask struct {
Data []byte
Workspace []byte
N int
M int
Result chan []byte
}
func ConcurrentCombinerSelect(result chan []byte, taskQue chan *CombineTask, data, workspace []byte, m, n int) {
for i := 0; i < len(data); i++ {
copyWorkspace := make([]byte, len(workspace))
copy(copyWorkspace, workspace)
copyWorkspace = append(copyWorkspace, data[i])
if len(copyWorkspace) == n {
//log(copyWorkspace)
result <- copyWorkspace
continue
}
copyData := data[i+1:]
if len(copyData)+len(copyWorkspace) < n {
continue
}
task := &CombineTask{
Data: copyData,
Workspace: copyWorkspace,
N: n,
M: m,
Result: result,
}
taskQue <- task
}
}
func CombinerSelectNoRecursion(atable []int32, n int) [][]int32 {
cnt := len(atable)
if n > len(atable) {
return nil
}
meta := make([]int32, cnt)
//init meta data
for i := 0; i < cnt; i++ {
if i < n {
meta[i] = 1
} else {
meta[i] = 0
}
}
var result [][]int32
//记录一次组合
var tmp []int32
for i := 0; i < cnt; i++ {
if meta[i] == 1 {
tmp = append(tmp, atable[i])
}
}
result = append(result, tmp)
for {
//前面连续的0
zero_count := 0
for i := 0; i < cnt-n; i++ {
if meta[i] == 0 {
zero_count = zero_count + 1
} else {
break
}
}
// 前m-n位都是0,说明处理结束
if zero_count == cnt-n {
break
}
var idx int
for j := 0; j < cnt-1; j++ {
// 10 交换为 01
if meta[j] == 1 && meta[j+1] == 0 {
meta[j], meta[j+1] = meta[j+1], meta[j]
idx = j
break
}
}
// 将idx左边所有的1移到最左边
var k = idx
var count = 0
for count <= k {
for i := k; i >= 1; i-- {
if meta[i] == 1 {
meta[i], meta[i-1] = meta[i-1], meta[i]
}
}
count = count + 1
}
// 记录一次组合
var tmp []int32
for i := 0; i < cnt; i++ {
if meta[i] == 1 {
tmp = append(tmp, atable[i])
}
}
result = append(result, tmp)
}
return result
}
func CombinerSelectUseRecursion(atable []int32, n int) [][]int32 {
var r [][]int32
var f func(t, a []int32, num int)
f = func(t, a []int32, num int) {
if num == 0 {
r = append(r, t)
return
}
l := len(a)
for i := 0; i <= l-num; i++ {
tt := make([]int32, len(t), n)
copy(tt, t)
tt = append(tt, a[i])
f(tt, a[i+1:], num-1)
}
}
f([]int32{}, atable, n)
return r
}
//全排列
func FullPermutation(atable []int32) [][]int32 {
var r [][]int32
var f func(a []int32, idx int)
f = func(a []int32, idx int) {
if idx >= len(a) {
dst := make([]int32, len(atable))
copy(dst, a)
r = append(r, dst)
return
}
for i := idx; i < len(a); i++ {
a[i], a[idx] = a[idx], a[i]
f(a, idx+1)
a[i], a[idx] = a[idx], a[i]
}
}
f(atable, 0)
return r
}