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
| func maxRepeating(sequence string, word string) (ans int) { dp := make([]int, len(sequence)) next := getNext(word) j := 0 for i := 0; i < len(sequence); i++ { for j > 0 && sequence[i] != word[j] { j = next[j - 1] } if sequence[i] == word[j] { j++ } if j == len(word) { if i - len(word) < 0{ dp[i] = 1 } else { dp[i] = dp[i - len(word)] + 1 } j = next[j - 1] } if dp[i] > ans { ans = dp[i] } } return ans }
func getNext(s string) []int { j := 0 next := make([]int, len(s)) next[0] = j for i := 1; i < len(s); i++ { for j > 0 && s[i] != s[j] {
j = next[j-1] } if s[i] == s[j] { j++ } next[i] = j } return next }
|