/
slice_lcs.go
175 lines (143 loc) · 3.39 KB
/
slice_lcs.go
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
package slice
// LCSMap 表示 LCS 计算结果图
type LCSMap struct {
M [][]int
L [][]LCSLink
maxX int
maxY int
equal EqualFunc
}
// LCSLink 表示
type LCSLink struct {
Down bool
LowerRight bool
Right bool
}
type EqualFunc func(xIndex, yIndex int) bool
// LCS 计算最长公共子序列,并返回结果图
func LCS(lenX, lenY int, equal EqualFunc) (m *LCSMap) {
m = makeLCSMap(lenX, lenY)
if lenX == 0 || lenY == 0 || equal == nil {
return m
}
m.equal = equal
for i := lenX; i >= 0; i-- {
for j := lenY; j >= 0; j-- {
m.ste(i, j)
}
}
return m
}
func makeLCSMap(lx, ly int) *LCSMap {
m := make([][]int, lx+1)
l := make([][]LCSLink, lx+1)
for i := range m {
m[i] = make([]int, ly+1)
l[i] = make([]LCSLink, ly+1)
}
res := &LCSMap{
M: m,
L: l,
maxX: lx,
maxY: ly,
}
return res
}
// ste 状态转移方程
func (m *LCSMap) ste(i, j int) int {
if i == m.maxX || j == m.maxY {
m.M[i][j] = 0
return 0
}
if m.equal(i, j) {
m.M[i][j] = m.M[i+1][j+1] + 1
m.L[i][j].LowerRight = true
return m.M[i][j]
}
if m.M[i+1][j] == m.M[i][j+1] {
m.M[i][j] = m.M[i+1][j]
m.L[i][j].Right, m.L[i][j].Down = true, true
} else if m.M[i+1][j] > m.M[i][j+1] {
m.M[i][j] = m.M[i+1][j]
m.L[i][j].Down = true
} else {
m.M[i][j] = m.M[i][j+1]
m.L[i][j].Right = true
}
if m.M[i][j] == 0 {
m.L[i][j].Right, m.L[i][j].Down, m.L[i][j].LowerRight = false, false, false
}
return m.M[i][j]
}
// Route 表示一个结果路径
type Route struct {
XIndexes []int
YIndexes []int
}
// RouteIter 组合起左右 LCS 的结果, 并且使用 [2]int 标记当前路径点属于那一边的值。
// -1 表示不存在
type RouteIter [][2]int
// ToIter 转为 RouteIter 格式
func (r *Route) ToIter(xLen, yLen int) RouteIter {
res := make(RouteIter, 0, len(r.XIndexes)+len(r.YIndexes))
xCurr, yCurr := 0, 0
for n, xIndex := range r.XIndexes {
for i := xCurr; i < xIndex; i++ {
res = append(res, [2]int{i, -1})
}
yIndex := r.YIndexes[n]
for j := yCurr; j < yIndex; j++ {
res = append(res, [2]int{-1, j})
}
res = append(res, [2]int{xIndex, yIndex})
xCurr, yCurr = xIndex+1, yIndex+1
}
for i := xCurr; i < xLen; i++ {
res = append(res, [2]int{i, -1})
}
for j := yCurr; j < yLen; j++ {
res = append(res, [2]int{-1, j})
}
return res
}
// GetARoute 方便地返回一个相同序列的路径。从 map 中的右下角,优先向上方搜索
func (m *LCSMap) GetRoute() *Route {
if m.maxX == 0 || m.maxY == 0 {
return &Route{}
}
if m.M[0][0] == 0 {
return &Route{}
}
res := m.trace(0, 0, &Route{})
return res[0]
}
// MaxSubLen 返回最大子序列计算结果
func (m *LCSMap) MaxSubLen() int {
if m.maxX == 0 || m.maxY == 0 {
return 0
}
return m.M[0][0]
}
func (m *LCSMap) trace(i, j int, r *Route) (res []*Route) {
// 等于0了,结束
if m.M[i][j] == 0 {
return []*Route{r}
}
// 有一个相等的节点,append
if m.L[i][j].LowerRight {
r.XIndexes = append(r.XIndexes, i)
r.YIndexes = append(r.YIndexes, j)
// logf(f, "%p - process: (%d, %d) r.XIndexes: %v, r.YIndexes: %v", r, i, j, r.XIndexes, r.YIndexes)
return m.trace(i+1, j+1, r)
}
// 没有相等的节点,那么看看有没有分叉
if m.L[i][j].Down && !m.L[i][j].Right {
res = m.trace(i+1, j, r)
} else if !m.L[i][j].Down && m.L[i][j].Right {
res = m.trace(i, j+1, r)
} else {
res = m.trace(i, j+1, r)
// res = m.trace(i+1, j, r)
}
return res
}