Skip to content

Commit 4ef3def

Browse files
committed
add subsequenceAutomaton
1 parent 7da6ca7 commit 4ef3def

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

copypasta/strings.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,37 @@ func stringCollection() {
218218
return s[i : i+n]
219219
}
220220

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+
221252
// 最长回文子串 Manacher
222253
// 推荐 https://www.bilibili.com/video/BV1AX4y1F79W
223254
// https://www.bilibili.com/video/BV1ft4y117a4
@@ -652,6 +683,7 @@ func stringCollection() {
652683
kmpSearch, calcMinPeriod,
653684
zSearch,
654685
smallestRepresentation,
686+
subsequenceAutomaton,
655687
manacher,
656688
suffixArray, suffixArrayInt, suffixArrayInt2,
657689
}

0 commit comments

Comments
 (0)