Data structure-4.4 strings, arrays and generalized tables

Preface-Data Structure

The data structure needs to be chewed repeatedly, and the answers to the problems encountered in the development can be obtained at any time.

Pattern matching

Exercise 1-BF pattern matching

  • Set the target string s = "eddede" and the pattern string t = "cdc". The length of s is n (n 6), and the length of t is m (m=3). The pointer i is used to indicate the current comparison character position of the target string s, and the pointer j is used to indicate the current comparison character position of the pattern string t. The BF pattern matching process is shown below.
    Insert picture description here

Exercise 2-KMP pattern matching

  • Exercise 2: Set the target string s="aabaaab", the pattern string -*aaab". The length of s is n(n=9) and the length of t is m(m=5). Use a pointer to indicate the current comparison character position of the target string s , Use the pointer to indicate the current comparison character position of the pattern string t, the KMP pattern matching process is shown below.
    Insert picture description here

Improvements to next nextvalue

Insert picture description here

to sum up

  • Because next[j] = k
  • pj = pk (that is, the characters are equal), so nextvalue[j] = nextvalue[k] = next[k] ,pj != pk, so nextvalue[j] = next[j],

example:

  • 因为next[4] = 1, a4 = a1 = 0, next[1] = 0, nextval[4] = next[1] = 0;

  • topic
    Insert picture description here

  • answer
    Insert picture description here

  • The time complexity of KMP is still O(n+m)

Guess you like

Origin blog.csdn.net/weixin_41732253/article/details/109547194