String Matching Based on the KMP algorithm (continued) --- restore the next Array

Source: Internet
Author: User

I believe that through today's article, you will have a deeper understanding of KMP, not just on the level of computing.

Through the first article above, I learned how to find the next array. I believe many people who like to answer questions will ask, and I can solve this problem according to your practice, so what is the next array? Why is it that?

Why is next array so? Today, when I read the introduction to algorithms, I found that the next array iteration calculation is correct and can understand some points, but it cannot be written. I also browsed July's article roughly, I want to see how he introduced it. I found that he skipped this part. At the end of the article, it took a year to fully understand it. Check whether I am still at a level that is not fully understood, so this part (the reason for the next array calculation) decides to put it down for a while (it is estimated that it will take a long time, because the next time will be very tight) when you have the energy to learn again, Add.

So today, let's talk about the next array as a magic horse? This article starts with the last part of Ruan Yifeng's article. If you do not know KMP or have not read my first KMP article, it is better to take 5 to 10 minutes to read it first. 1. Take "ABCDABD" as an example to understand the prefix and Suffix:
-The prefix and suffix of "A" are empty sets. The length of the common element is 0. the prefix of-"AB" is [A] and the suffix is [B]. the total element length is 0. the prefix of-"ABC" is [A, AB], the suffix is [BC, C], and the total element length is 0; -The prefix of "ABCD" is [A, AB, ABC], and the suffix is [BCD, CD, D]. The length of all elements is 0; -The prefix of "ABCDA" is [A, AB, ABC, ABCD], and the suffix is [BCDA, CDA, DA, A]. The total element is "A" and the length is 1; -The prefix of "ABCDAB" is [A, AB, ABC, ABCD, ABCDA], and the suffix is [BCDAB, CDAB, DAB, AB, B]. The total element is "AB ", the prefix of-"ABCDABD" is [A, AB, ABC, ABCD, ABCDA, ABCDAB], and the suffix is [BCDABD, CDABD, DABD, ABD, BD, d]. The length of all elements is 0.

IIThe next array stores the "prefix" and "suffix" of the previously matched characters in the pattern string. Length of the longest co-occurrence Element. The concept is relatively round. Note that space is used in the above sentence to help you understand the concept. The following example shows that the pattern string T = "ABCDABD" is used to find next [j, we assume that T [0] -- T [J-1] can all be matched successfully. This assumption is reasonable, because the next array is used to obtain the starting position of the next matching only when the matching fails. Read it several times. Well, let's find next [1]. The character that has been matched in the pattern string is T [0] = A. In the first part, the prefix and suffix of the pattern string are all null, the total element length is 0, so next [1] = 0; find next [4], the character previously matched in the mode string is T [0] -- T [3] = {A, B, C, D }, from the first part, the total element length of its prefix and suffix is 0, so next [4] = 0; find next [5], the character previously matched in the mode string is T [0] -- T [4] = {A, B, C, D, }, from the first part, get his prefix and suffix. The total element is 'A' and the length is 1, so next [5] = 1; well, the other ones will try to push it down.
III,Next, another understanding of the array (a little preparation for the BM algorithm after learning). Suppose there is a match below to pay attention to the two AB S in the color on our graph. When the D match fails, the next array is actually used to move the previous AB to the matched AB, for example, to avoid backtracking and accelerate efficiency. In the BM algorithm I Am Going To introduce later, this character string is called a good character string. Haha, is it too knowledgeable? Well, today's introduction ends here, in this way, KMP is completely introduced.

If you think this article has benefited you, please help.
In addition, I have activated a public account to share the beauty of technology. I will share some things I learned from time to time. You can search for a public account: SwalgeOr scan the QR code below to follow me
(Repost please indicate the source: http://blog.csdn.net/swagle/article/details/24112823)

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.