func getNext(t []string) []int {
tLen := len(t)
next := make([]int, tLen)
for i, _ := range next {
next[i] = -1
}
for i,j := 1,-1; i < tLen ; i++ {
for j != -1 && t[j + 1] != t[i] {
j = next[j]
}
if t[j + 1] == t[i] {
j++
}
next[i] = j
}
fmt.Println(next)
return next
}
func kmp(s []string, t []string) int {
next := getNext(t)
sIndex, tIndex := 0, 0
for sIndex < len(s) && tIndex < len(t) {
if tIndex != 0 && s[sIndex] != t[tIndex] {
tIndex=next[tIndex-1] + 1
} else if tIndex == 0 && s[sIndex] != t[tIndex] {
sIndex++
} else {
sIndex++
tIndex++
}
}
fmt.Println(tIndex)
fmt.Println(sIndex)
if tIndex == len(t) {
return sIndex - tIndex
}
return -1
}
func main() {
s := []string{"G","T","G","T","G","A","G","C","T","G","G","T","G","T","G","T","G","C","F","A","A"}
t := []string{"G","T","G","T","G","C","F"}
fmt.Println(kmp(s, t))
}