[Original] simple mode matching and improved mode matching (KMP) algorithm description (draft)

Source: Internet
Author: User

Simple mode matching and improved mode matching (KMP) Algorithm Description
Lewislau
Recently, I reviewed the data structure. In the past, the instructor ignored the strings. Khan, our school is indeed a cow B. One of my friends told me that KMP is basically a difficult algorithm in the data structure, so I was at least psychologically encouraged to master it, however, this algorithm is mastered by the teacher. Let's get started with the KMP algorithm.
Before talking about the improved KMP algorithm, let's talk about simple pattern matching:
In fact, it is very simple, that is, two strings are compared by bit. In pattern matching: Assume that string P is matched in string T. In this case, P is the pattern string and T is the target string.
OK. I generally prefer to use instances to describe the problem.
T: A B D A B C
P: A B D A B C

Simple pattern matching algorithm

The simple pattern matching algorithm is compared by P and T in sequence, that is:
First comparison: T: A B D A B C
P: A B D A B C
It is found that 6th elements (subscript: 5) D and C are not equal. The first line ends, and 6 times (6 elements) are compared ).
The second comparison: T: A B D A B C
P: A B D A B C
The first element is not equal, and the second end is compared once (one element ).
Third comparison: T: A B D A B C
P: A B D A B C
The first element is not equal. The third end is compared once (one element ).
The fourth comparison: T: A B D A B C
P: A B D A B C
The first element is equal, the second element is equal, and the third, fourth, fifth, and sixth elements are equal. The match is successful, and the fourth element ends. At this time, 6 times (6 elements) are compared ).
Matched successfully, compared 14 times in total. But this is our ideal match scheme. In reality, the length of the string is far more than that. This algorithm is a back-to-inverse algorithm and becomes a simple pattern matching algorithm.
Assume that the target t length is n and the mode P length is M. In the worst case, the number of comparisons can reach:
(N-m + 1) * m times. In many cases, M is much smaller than N, and its time complexity is O (n * m ).

Improved mode matching (KMP) Algorithm

The KMP algorithm eliminates the Back-to-inverse of Simple Matching and uses a failure function to replace direct back-to-back. The idea is as follows:
First comparison: T: A B D A B C
P: A B D A B C
It is found that 6th elements (subscript: 5) D and C are not equal. At this time, enter the processing of a p string:
In this case, the p string is extracted. a B d a B C does not match D because C does not match D.
A B D A B
In this case, determine whether a B d a is equal to B d a B? Wait, go to the next round of judgment
In this case, determine whether a B d is equal to d a B? Wait, go to the next round of judgment
In this case, determine whether a B is equal to a B? Equal. End the first overall judgment.
(Don't worry about it first. Next I will explain why this match matches the purpose of this match !)
The above is the KMP process. Why? In some strings, the target string is much longer than the mode string. If the target string and the mode string are compared one by one each time. At this time, the time complexity increases, and many invalid matches will appear in the mode string, which is equivalent to useless. However, if we compare the mode strings first, because the mode strings are much shorter than the target strings, it will reduce the time complexity.
The above is a brief introduction to KMP. We have the opportunity to sort out detailed algorithms and their advantages!
I don't want to talk about anything else. I just want to say that "algorithms are the soul of the program !" This old and classic story !!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.