Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix:修复好后缀的前缀被跳过的情况 #536

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions go/32_string/string_bm.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"math"
)

//bc: pattern char index hash mapping
// bc: pattern char index hash mapping
func generateBC(pattern string) []int {

bc := make([]int, 256)
Expand All @@ -21,7 +21,7 @@ func generateBC(pattern string) []int {
return bc
}

//generate suffix and prefix array for pattern
// generate suffix and prefix array for pattern
func generateGS(pattern string) ([]int, []bool) {
m := len(pattern)
suffix := make([]int, m)
Expand Down Expand Up @@ -50,7 +50,7 @@ func generateGS(pattern string) ([]int, []bool) {
return suffix, prefix
}

//todo
// todo
func moveByGS(patternLength int, badCharStartIndex int, suffix []int, prefix []bool) int {

//length of good suffix
Expand All @@ -61,13 +61,12 @@ func moveByGS(patternLength int, badCharStartIndex int, suffix []int, prefix []b
return badCharStartIndex + 1 - suffix[k]
}

//partial match
for t := patternLength - 1; t > badCharStartIndex+1; t-- {
if prefix[t] {
return t
//search prefix
for k--; k > 0; k-- {
if prefix[k] {
return patternLength - k
}
}

//no match
return patternLength

Expand Down