"Algorithm"-string pattern matching algorithm 1 [BF algorithm]

introduction

String pattern matching:

\quad \quad Suppose S and T are two given strings. The process of finding the pattern string T in the main string S is called string matching. If the pattern string T is found in the main string S, it is said that the match is successful, and the function returns T in S The first occurrence of the position , otherwise the match is unsuccessful, and -1 is returned.

Example:
Insert picture description here
In the above figure, we are trying to find the pattern string T = baab. The position where the pattern string appears for the first time in the main string S = abcabaabcabac is the red shaded part, and the position where T appears for the first time in S is subscript 4 (The first subscript of the string is 0), so 4 is returned. If the pattern string T does not appear in the main string S, then -1 is returned.

1. Basic idea

BF algorithm, namely Brute Force (Brute Force algorithm), also known as the naive algorithm .

Basic idea:

\quad \quad Starting from the first character of the main string S and the first character of the pattern T, if they are equal, continue to compare the subsequent characters of the latter; otherwise, starting from the second character of the main string S and the first character of the pattern T One character is compared, and the above process is repeated until all the characters in T are compared, which means that the match is successful; or all characters in S are late, which means that the match fails.

2. Pseudo code

1. The initial subscripts i=0 and j=0 in string S and string T.
2. Loop until all characters of S or T are not compared:

  • 2.1 If S[i]==T[j], continue to compare the next character of S and T
  • 2.2 Otherwise, trace i back to i-j+1, and trace j back to 0, and compare again

3. If all characters in T are compared, the match is successful and the starting index of the match is returned; otherwise, the match fails and -1 is returned.

3. Code implementation

def BF(s,t):
    i=0
    j=0
    while i<len(s) and j<len(t):
        if s[i]==t[j]:
            i+=1
            j+=1
        else:
            i=i-j+1
            j=0
    if j>=len(t):
        return i-len(t)
    else:
        return -1
if __name__=="__main__":
    s="abcabaabcabac"
    t="baab"
    b=BF(s,t)
    print(b)
  • Time complexity: O (m ∗ n) O(m*n)O ( mn ) , where m and n are the lengths of the main string and pattern string respectively
  • Space complexity: O (1) O (1)O ( 1 )

Guess you like

Origin blog.csdn.net/weixin_45666566/article/details/112440440