-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
56 lines (47 loc) · 630 Bytes
/
main.go
File metadata and controls
56 lines (47 loc) · 630 Bytes
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
package main
import "fmt"
func main() {
t := "asbasdasadasdawesafbasdasbasd"
p := "aabaabaaa"
pre := make([]int, len(p))
// preprocess
pre[0] = 0
i := 1
j := 0
for i < len(p) {
if p[i] == p[j] {
j++
pre[i] = j
i++
} else {
if j != 0 {
j = pre[j-1]
} else {
pre[i] = 0
i++
}
}
}
i = 0
j = 0
for i < len(t) {
if p[j] == t[i] {
i++
j++
}
if j == len(p) {
fmt.Println(i - j)
j = pre[j-1]
} else if i < len(t) && p[j] != t[i] {
if j != 0 {
j = pre[j-1]
} else {
i++
}
}
}
fmt.Println(p)
fmt.Println(pre)
}
//a s b s a s d
//0 0 0 0 1 2