KMP Code

 1
 2func kmp(s, sub string) int {
 3	bu := make([]int, len(sub))
 4	// abcdabd
 5	for i, j := 1, 0; i < len(sub); i++ {
 6		for j > 0 && sub[j] != sub[i] {
 7			j = bu[j-1]
 8		}
 9		if sub[j] == sub[i] {
10			j++
11		}
12		bu[i] = j
13	}
14    for i, j := 0, 0; i < len(s); i++ {
15		for j > 0 && s[i] != sub[j] {
16			j = bu[j-1]
17		}
18		if s[i] == sub[j] {
19			j++
20		}
21		if j == len(sub) {
22			return i - j + 1
23		}
24	}
25	return -1
26}
27
28func TestKmp(t *testing.T) {
29	// kmp("BBC ABCDAB ABCDABCDABDE", "ABCDABD")
30	var a []int
31	var b chan int
32	if a == nil {
33		fmt.Print(a)
34	} else {
35		fmt.Print(b)
36	}
37}
38

K8s Questions
Golang Happened-Before
comments powered by Disqus