美文网首页
暴力字符串匹配查找

暴力字符串匹配查找

作者: Poisson_Lee | 来源:发表于2020-02-11 15:05 被阅读0次

https://github.com/TheAlgorithms/Python/blob/master/strings/naive_string_search.py

返回匹配位置 列表:

"""
this algorithm tries to find the pattern from every position of
the mainString if pattern is found from position i it add it to
the answer and does the same for position i+1
Complexity : O(n*m)
    n=length of main string
    m=length of pattern string
"""


def naivePatternSearch(mainString, pattern):
    patLen = len(pattern)
    strLen = len(mainString)
    position = []
    for i in range(strLen - patLen + 1):
        match_found = True
        for j in range(patLen):
            if mainString[i + j] != pattern[j]:
                match_found = False
                break
        if match_found:
            position.append(i)
    return position

mainString = "ABAAABCDBBABCDDEBCABC"
pattern = "ABC"
position = naivePatternSearch(mainString, pattern)
print(position)

输出

[4, 10, 18]

相关文章

  • KMP算法文章合集

    字符串的查找:朴素查找算法和KMP算法 暴力匹配算法与KMP算法(串的匹配) 字符串查找算法BF和KMP 字符串匹...

  • 子字符串查找(1)

    一、定义 本文主要介绍子字符串查找的各类常用算法,如朴素匹配算法(暴力查找)、KMP算法、BM算法等。各类匹配算法...

  • KMP字符串匹配算法的实现

    KMP字符串匹配算法的实现 暴力查找 这是最简单的一种字符串匹配算法: 使用一个指针 i 跟踪目标文本 txt, ...

  • 暴力字符串匹配查找

    https://github.com/TheAlgorithms/Python/blob/master/strin...

  • 数据结构与算法 -- 串,BF算法和RK算法

    BF算法 BF(Brute Force)算法,即暴力匹配算法 如果在字符串A中查找字符串B,那么字符串A就是主串,...

  • 给初学者,最易读懂的 KMP 模式匹配算法详解

    目的 已知字符串 S 和 T,查找 T 在 S 中的位置。 暴力匹配法 1-1、从下标 0 位置开始匹配,S[0]...

  • KMP算法

    KMP算法用于子字符串查找(匹配)。 KMP是三个科学家[Knuth-Morris-Pratt]发明的,旨在对暴力...

  • 字符串匹配

    字符串匹配(string matching)也称字符串查找(string searching)或模式匹配(patt...

  • 字符串匹配

    indexOf 底层就是使用字符串匹配算法 字符串匹配算法很多 BF( Brute Force)算法 暴力匹配算...

  • iOS 字符串

    1、字符串的截取 2、匹配字符串 从字符串(sd是sfsfsAdfsdf)中查找(匹配)字符串(Ad) 3、字符串...

网友评论

      本文标题:暴力字符串匹配查找

      本文链接:https://www.haomeiwen.com/subject/whjxfhtx.html