You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

41 lines
864 B

package main
import (
"fmt"
)
/**
暴力匹配方法的思想非常朴素:
1、依次从主串的首字符开始,与模式串逐一进行匹配;
2、遇到失配时,则移到主串的第二个字符,将其与模式串首字符比较,逐一进行匹配;
3、重复上述步骤,直至能匹配上,或剩下主串的长度不足以进行匹配。
*/
func main() {
fmt.Println("暴力匹配 字符串信息!")
strA := "ababcabcacbab"
strB := "abcac"
ii := brute_force_match(strA, strB)
fmt.Println("截至到:", ii)
}
func brute_force_match(strA, strB string) int {
strALen := len(strA)
strBLen := len(strB)
var i, j, strCLen int
for ; i <= strALen-strBLen; i++ {
j = 0
strCLen = i
for {
if strA[strCLen] == strB[j] && j < strBLen {
strCLen++
j++
}
break
}
if j == strBLen {
return i
}
}
return -1
}