BruteForceStringMatching2

来源:互联网 发布:淘宝网韩版针织开衫 编辑:程序博客网 时间:2024/06/10 06:09
//The brute-force algorithm is to solve the problem of //counting,in a given text, the number of substrings that starts //with an A and ends with a B.//For example,there are four such substrings in CABAAXBYA.package mainimport (    "fmt")func StringMatching2(text []rune) int {    n := len(text)    count := 0    for i := 0; i <= n-2; i++ {        for j := 0; i+j < n; {            if j == 0 {                if text[i+j] == 'A' {                    j++                    continue                } else {                    break                }            } else {                if text[i+j] == 'B' {                    count++                }                j++            }        }    }    return count}func main() {    text := []rune("CABBBBB")    fmt.Println(StringMatching2(text))}
0 0
原创粉丝点击