File tree Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change @@ -218,6 +218,37 @@ func stringCollection() {
218
218
return s [i : i + n ]
219
219
}
220
220
221
+ // LC周赛259D https://leetcode-cn.com/problems/longest-subsequence-repeated-k-times/
222
+ subsequenceAutomaton := func (s string ) {
223
+ // build nxt
224
+ // nxt[i][j] 表示在 i 右侧的字符 j 的最近位置
225
+ pos := [26 ]int {}
226
+ for i := range pos {
227
+ pos [i ] = len (s )
228
+ }
229
+ nxt := make ([][26 ]int , len (s ))
230
+ for i := len (s ) - 1 ; i >= 0 ; i -- {
231
+ nxt [i ] = pos
232
+ pos [s [i ]- 'a' ] = i
233
+ }
234
+
235
+ // 返回是 s 的子序列的最长的 t 的前缀的长度
236
+ match := func (t string ) int {
237
+ i , j := 0 , 0
238
+ if t [0 ] == s [0 ] {
239
+ j = 1
240
+ }
241
+ for ; j < len (t ); j ++ {
242
+ i = nxt [i ][t [j ]- 'a' ]
243
+ if i == len (s ) {
244
+ break
245
+ }
246
+ }
247
+ return j
248
+ }
249
+ _ = match
250
+ }
251
+
221
252
// 最长回文子串 Manacher
222
253
// 推荐 https://www.bilibili.com/video/BV1AX4y1F79W
223
254
// https://www.bilibili.com/video/BV1ft4y117a4
@@ -652,6 +683,7 @@ func stringCollection() {
652
683
kmpSearch , calcMinPeriod ,
653
684
zSearch ,
654
685
smallestRepresentation ,
686
+ subsequenceAutomaton ,
655
687
manacher ,
656
688
suffixArray , suffixArrayInt , suffixArrayInt2 ,
657
689
}
You can’t perform that action at this time.
0 commit comments