登录
首页 >  Golang >  Go问答

出现问题:在字符串中搜索子字符串

来源:stackoverflow

时间:2024-02-08 10:30:22 487浏览 收藏

对于一个Golang开发者来说,牢固扎实的基础是十分重要的,golang学习网就来带大家一点点的掌握基础知识点。今天本篇文章带大家了解《出现问题:在字符串中搜索子字符串》,主要介绍了,希望对大家的知识积累有所帮助,快点收藏起来吧,否则需要时就找不到了!

问题内容

代码无法正常工作。

我必须检查给定字符串中包含的所有子字符串,并且它们必须满足这两个要求:

  • 长度至少等于 3 个字符;
  • 以相同字符开头和结尾。

输出必须包含这些子字符串及其出现次数。

输入abbabbaabcacba时没有问题,但输入时没有问题eabcacf 错误开始弹出。事实上,它会重新读取 cac 子字符串并再次对其进行计数,因此我尝试通过检查该子字符串的位置来解决此问题(strings.index 函数)并在之后修剪字符串,所以如果子字符串需要再次重新读取,那么肯定是由于发生而不是由于错误......至少这是我的想法。

...因为在给出eabbcabcbf时,即使在之前的修复之后,它也会重新读取cabc,这是由于 cabc 出现两次但不是连续出现而导致的,因此 previous/current 方法不起作用,必然需要 previous2/previous1/current 方法来检查倒数第三个子字符串,但我认为这样总会出现错误,并且无法修复。

也许问题是由在开头查找所有子字符串的代码部分的可能错误实现引起的,但我不知道如何修复它。

我附上了下面的代码和输出。

希望有人能帮助我!

代码 (我放置了一些fmt.println()函数来打印切片并进行调试)

package main

import (
    "fmt"
    "os"
    "strings"
)

func main() {
    string1 := os.args[1]
    count1 := 0
    count2 := 0
    count3 := 0
    var substring1, substring2, start1, previous1, output1 string
    var slice1, slice2, slice5 []string
    var slice3, slice4 []int
    var position1, saved1 int
    var check1 bool
    dict1 := make(map[string]int)
    for index1 := 3; index1 <= len(string1); index1++ {
        for index2 := 0; index2 <= len(string1)-3; index2++ {
            for _, value3 := range string1[index2:] {
                count1++
                substring1 += fmt.sprintf("%s", string(value3))
                if count1 == index1 {
                    slice1 = append(slice1, substring1)
                    count1 = 0
                    substring1 = ""
                }
            }
            count1 = 0
            substring1 = ""
        }
    }
    fmt.println(slice1)
    for _, value4 := range slice1 {
        substring2 = value4
        for index5, value5 := range substring2 {
            if index5 == 0 {
                start1 = string(value5)
            }
            if (index5 == len(substring2)-1) && (string(value5) == start1) {
                slice2 = append(slice2, substring2)
            }
        }
    }
    fmt.println(slice2)
    for index6, value6 := range slice2 {
        if index6 == 0 {
            previous1 = value6
        }
        if (value6 == previous1) && (index6 == 0) {
            position1 = strings.index(string1[position1:], value6)
            saved1 = position1
            slice3 = append(slice3, position1)
        } else if (value6 == previous1) && (index6 != 0) {
            position1 = strings.index(string1[saved1+1:], value6)
            slice3 = append(slice3, position1)
        } else if value6 != previous1 {
            position1 = 0
            position1 = strings.index(string1[position1:], value6)
            saved1 = position1
            slice3 = append(slice3, position1)
        }
        previous1 = value6
    }
    fmt.println(slice3)
    for index7, value7 := range slice3 {
        if value7 == -1 {
            slice4 = append(slice4, index7)
        }
    }
    fmt.println(slice4)
    if len(slice4) > 0 {
        for index8, value8 := range slice2 {
            for _, value9 := range slice4 {
                if index8 != value9 {
                    check1 = false
                } else {
                    check1 = true
                    break
                }
            }
            if check1 == false {
                slice5 = append(slice5, value8)
            }
        }
        fmt.println(slice5)
        for _, value10 := range slice5 {
            dict1[value10] = dict1[value10] + 1
        }
        for range dict1 {
            count2++
        }
        for key1, item1 := range dict1 {
            count3++
            if count3 != count2 {
                output1 += fmt.sprintf("%s -> occorrenze: %d\n", key1, item1)
            } else {
                output1 += fmt.sprintf("%s -> occorrenze: %d", key1, item1)
            }
        }
        fmt.println(output1)
    } else {
        for _, value8 := range slice2 {
            dict1[value8] = dict1[value8] + 1
        }
        for range dict1 {
            count2++
        }
        for key1, quantity1 := range dict1 {
            count3++
            if count3 != count2 {
                output1 += fmt.sprintf("%s -> occurrences: %d\n", key1, quantity1)
            } else {
                output1 += fmt.sprintf("%s -> occurrences: %d", key1, quantity1)
            }
        }
        fmt.println(output1)
    }
}

输出

正确

$ go run exercise_2.go abbabba
abbabba -> occurrences: 1
bbabb -> occurrences: 1
babb -> occurrences: 1
abba -> occurrences: 2
bbab -> occurrences: 1
bab -> occurrences: 1

正确

$ go run exercise_2.go abcacba
abcacba -> occurrences: 1
bcacb -> occurrences: 1
abca -> occurrences: 1
acba -> occurrences: 1
cac -> occurrences: 1

正确但由于方法不当而实际上不正确

$ go run exercise_2.go eabcacf
abca -> occurrences: 1
cac -> occurrences: 1

不正确

$ go run exercise_2.go eabbcabcbf
bbcabcb -> Occurrences: 1
bcabcb -> Occurrences: 1
abbca -> Occurrences: 1
bbcab -> Occurrences: 1
cabc -> Occurrences: 2
bcab -> Occurrences: 1
bcb -> Occurrences: 1

⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀


正确答案


感谢@rocka2q帮助我!这是代码的调整后的最终版本。

正确的代码,完整的版本(因为我需要它工作)

package main

import (
    "fmt"
    "os"
)

func main() {
    string1 := os.args[1]
    map2 := substrings(string1)
    count1 := 0
    count2 := 0
    var output1 string
    for range map2 {
        count1++
    }
    for key1, quantity1 := range map2 {
        count2++
        if count2 != count1 {
            output1 += fmt.sprintf("%s -> occurrences: %d\n", key1, quantity1)
        } else {
            output1 += fmt.sprintf("%s -> occurrences: %d", key1, quantity1)
        }
    }
    if len(map2) != 0 {
        fmt.println(output1)
    }
}

func substrings(string1 string) map[string]int {
    map1 := make(map[string]int)
    for index1 := 0; index1 < len(string1)-2; index1++ {
        for index2 := index1 + 2; index2 < len(string1); index2++ {
            if string1[index1] == string1[index2] {
                map1[string1[index1:index2+1]]++
            }
        }
    }
    return map1
}

输出,全部正确

$ go run exercise_2.go abbabba
abbabba -> occurrences: 1
bbabb -> occurrences: 1
babb -> occurrences: 1
abba -> occurrences: 2
bbab -> occurrences: 1
bab -> occurrences: 1
$ go run exercise_2.go abcacba
abcacba -> occurrences: 1
bcacb -> occurrences: 1
abca -> occurrences: 1
acba -> occurrences: 1
cac -> occurrences: 1
$ go run exercise_2.go eabcacf
abca -> occurrences: 1
cac -> occurrences: 1
$ go run exercise_2.go eabbcabcbf
bbcabcb -> Occurrences: 1
bcabcb -> Occurrences: 1
abbca -> Occurrences: 1
bbcab -> Occurrences: 1
cabc -> Occurrences: 1
bcab -> Occurrences: 1
bcb -> Occurrences: 1

本篇关于《出现问题:在字符串中搜索子字符串》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于Golang的相关知识,请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>