-
Notifications
You must be signed in to change notification settings - Fork 641
/
Copy pathtrie.go
251 lines (232 loc) · 6.34 KB
/
trie.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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package copypasta
/* 前缀树/字典树/单词查找树
适用于多串前缀/后缀匹配
另类解读:如果将字符串长度视作定值 L 的话,trie 树是一种 O(nL) 排序,O(L) 查询的数据结构
https://oi-wiki.org/string/trie/
https://www.quora.com/q/threadsiiithyderabad/Tutorial-on-Trie-and-example-problems
https://algs4.cs.princeton.edu/code/edu/princeton/cs/algs4/TrieST.java.html
https://algs4.cs.princeton.edu/code/edu/princeton/cs/algs4/TrieSET.java.html
https://algs4.cs.princeton.edu/code/edu/princeton/cs/algs4/TST.java.html
注:由于用的是指针写法,必要时禁止 GC,能加速不少
func init() { debug.SetGCPercent(-1) }
模板题 LC208 https://leetcode.cn/problems/implement-trie-prefix-tree/
最长匹配后缀 https://leetcode.cn/problems/longest-common-suffix-queries/
前后缀同时匹配 LC745 https://leetcode.cn/problems/prefix-and-suffix-search/
LC3045 https://leetcode.cn/problems/count-prefix-and-suffix-pairs-ii/
- 把 (s[i], s[n-1-i]) 插入字典树
LC527 https://leetcode.cn/problems/word-abbreviation/
https://codeforces.com/contest/514/problem/C
回文对(配合 Manacher 可以做到线性复杂度)LC336 https://leetcode.cn/problems/palindrome-pairs/
与 DP 结合 https://leetcode.cn/problems/re-space-lcci/
与贪心堆结合 https://codeforces.com/problemset/problem/965/E
todo https://codeforces.com/contest/455/problem/B
深刻理解 https://atcoder.jp/contests/abc273/tasks/abc273_e
https://atcoder.jp/contests/abc353/tasks/abc353_e
*/
type trieNode struct {
son [26]*trieNode
cnt int // trieNode 对应的完整字符串的个数
sum int // 子树 cnt 之和
val int // 额外存储的信息
}
func (o *trieNode) empty() bool {
for _, son := range o.son {
if son != nil {
return false
}
}
return true
}
type trie struct{ root *trieNode }
func newTrie() *trie {
return &trie{&trieNode{}}
}
func (trie) ord(c rune) rune { return c - 'a' }
func (trie) chr(v byte) byte { return v + 'a' }
// 插入字符串 s,附带值 val,返回插入后字符串末尾对应的节点
func (t *trie) put(s string, val int) *trieNode {
o := t.root
for _, b := range s {
b = t.ord(b)
if o.son[b] == nil {
o.son[b] = &trieNode{}
}
o = o.son[b]
o.sum++ // 子树 cnt 之和(o 对应的字符串是多少个完整字符串的前缀)
}
o.cnt++ // o 对应的完整字符串的个数
o.val = val
return o
}
// 字典树 DFS(模板)
// LC2416 https://leetcode.cn/problems/sum-of-prefix-scores-of-strings/
func (t *trie) dfs() {
var f func(*trieNode, int)
f = func(o *trieNode, sum int) {
if o == nil {
return
}
// 统计从 root 到 o 的路径
sum += o.cnt
// ...
for _, child := range o.son {
f(child, sum)
}
}
f(t.root, 0)
}
// 字符串 s 与字典树中字符串的最长公共前缀
// 返回最后一个匹配的节点(最长公共前缀),以及是否找到 s
func (t *trie) find(s string) (*trieNode, bool) {
o := t.root
for _, b := range s {
nxt := o.son[t.ord(b)]
if nxt == nil {
return o, false
}
o = nxt
}
return o, o.cnt != 0
}
// 删除字符串 s,返回字符串末尾对应的节点
// LC1804 https://leetcode.cn/problems/implement-trie-ii-prefix-tree/
func (t *trie) delete(s string) *trieNode {
fa := make([]*trieNode, len(s))
o := t.root
for i, b := range s {
fa[i] = o
o = o.son[t.ord(b)]
if o == nil {
return nil
}
}
o.cnt--
if o.cnt == 0 {
for i := len(s) - 1; i >= 0; i-- {
f := fa[i]
f.son[t.ord(rune(s[i]))] = nil // 完全删除节点
if !f.empty() {
break
}
}
}
return o
}
// 求小于 s 的字符串个数
func (t *trie) rank(s string) (k int) {
o := t.root
for _, b := range s {
b = t.ord(b)
// 累加在 b 之前的子树大小
for _, son := range o.son[:b] {
if son != nil {
k += son.sum
}
}
o = o.son[b]
if o == nil {
return
}
k += o.cnt // 以 b 结尾的字符串个数
}
// 上面算的是小于等于 s 的字符串个数
// 要算小于 s 的字符串个数,要把恰好等于 s 的字符串个数减掉
k -= o.cnt
return
}
// 求第 k 小(k 从 0 开始)
// 相当于 <= s 的字符串至少有 k+1 个
// !需要保证 trie 中至少有 k+1 个字符串
// https://codeforces.com/problemset/problem/557/E
func (t *trie) kth(k int) (s []byte) {
o := t.root
outer:
for {
for i, son := range o.son {
if son == nil {
continue
}
// 子树 son 中的字符串都比答案小
if k >= son.sum {
k -= son.sum
continue
}
s = append(s, 'a'+byte(i))
k -= son.cnt
if k < 0 {
return
}
o = son
continue outer
}
panic("k is too large (make sure k starts from 0)")
}
}
// 结合 rank 和 kth,可以求出一个字符串的前驱和后继
// 见 bst.go 中的 prev 和 next
// 返回字符串 s 在 trie 中的前缀个数
// https://codeforces.com/gym/101628/problem/K
// https://www.acwing.com/problem/content/144/
func (t *trie) countPrefixOfString(s string) (cnt int) {
o := t.root
for _, b := range s {
o = o.son[t.ord(b)]
if o == nil {
return
}
cnt += o.cnt
}
return
}
// 返回 trie 中前缀为 p 的字符串个数
// 此时 o.endCnt 保存子树字符串个数
// https://codeforces.com/gym/101628/problem/K
// LC1804 https://leetcode.cn/problems/implement-trie-ii-prefix-tree/
func (t *trie) countStringHasPrefix(p string) int {
o := t.root
for _, b := range p {
o = o.son[t.ord(b)]
if o == nil {
return 0
}
}
return o.cnt
}
// s 的本质不同子串数量 O(n^2)
// 做法是插入每个后缀,统计节点数。但题目往往会带上额外的条件
// https://codeforces.com/problemset/problem/271/D
// - 注:这题还可以用后缀数组+前缀和二分来做到 O(nlogn)
func (t *trie) countDistinctSubstring(s string) (cnt int) {
for i := range s {
o := t.root
for _, b := range s[i:] {
b = t.ord(b)
if o.son[b] == nil {
o.son[b] = &trieNode{}
cnt++
}
o = o.son[b]
}
}
return
}
// EXTRA: 可持久化字典树
// 注意为了拷贝一份 trieNode,这里的接收器不是指针
// https://oi-wiki.org/ds/persistent-trie/
// roots := make([]*trieNode, n+1)
// roots[0] = &trieNode{}
// roots[i+1] = roots[i].put(s)
func (o trieNode) put(s []byte) *trieNode {
if len(s) == 0 {
o.cnt++
return &o
}
b := s[0] - 'a' //
if o.son[b] == nil {
o.son[b] = &trieNode{}
}
o.son[b] = o.son[b].put(s[1:])
//o.maintain()
return &o
}
// 扩展:见 acam.go 和 pam.go