Summary of KMP algorithm thinking

Source: Internet
Author: User
Tags string back

The KMP algorithm was first learned to be around the halo, but in fact, as long as the grasp of the logic of the idea, still very studious.

We set the main string to s and the substring to a

Now the first part matches, that is to say, the hook part of S string is exactly the same as the hook part of a string, but the S "I" is not the same as a "K". If we follow the conventional line of thinking, we will only move the A string back one (the process of the naïve algorithm can also be understood), but this time will be a lot of waste, how to optimize it?

Suppose, in the first part of the match area, a string exists such a substring (it is both at the head of a and at the tail of a, and X is as large as possible ...). In fact, to maximize), we call this substring x, so we can see that in the first part of the match, X has four regions, then, when the match occurs, the S "K"! =a "I", we can pan A, this time not to translate a unit, but to align the first x in a with the X in S. It saves a lot of time here.

Prove:

Assuming y exists (Y does not go to the boundary, which is different from X), the same as Y reaches the maximum, then the y of S is a bit, and the next one in the a must not be equal, because the largest, if equal, is not the biggest contradiction with Y, then we get, even if we move the string to the beginning of Y The string also does not match. And we have demonstrated the correctness of X (see the second picture ...). ), and we can choose, that is, X, Y, one of them, since it is doomed to be wrong, from the optimization considerations, the remaining X.

The next step is how to find the length of X, we set Next "" to save the length of X (in a), here we use the recursive method:

If A[next[i-1]+1=a[i "then next[i] = next[i-1]+1

Else find the second largest x, which is Y, recursive

Here's how the second-largest x is, the second-largest x satisfies the condition that the second largest is smaller than the first, which is the interior of the first big X. If the largest Y (the second largest x) is found inside X, it must satisfy the diagram, then Y can be asked.

This is the KMP algorithm.

Summary of KMP algorithm thinking

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.