Be careful when learning KMP Algorithms

Source: Internet
Author: User

The KMP algorithm is used to search for a word with a length of m in a document with N letters;
Brute-force enumeration: Compare the first M letters and words in the document, and then compare them with 2nd to m + 1, and then 3rd to m + 2; in this way, the complexity of the algorithm reaches O (M * n) at the worst, which is certainly not good for big data.
The essence of the KMP algorithm is to try to reduce unnecessary enumerations, for example; for example, the first K-1 letter that has matched the word; but the k letter cannot match; so if there is a situation similar to the background in the first K-1 letters (the substring consisting of the first I letter is the same as the substring consisting of the last I letter ), then the pointer J is changed to I (which is equivalent to moving the whole to the right) to reduce the number of enumerations. Therefore, you can pre-process the words to be searched, next [I] = t to save the word in front of the I-1 letters, the first t letters of the Child string and the last t letters of the Child string is the same; and T as big as possible;
How to find next [I? Initialize next [0] = next [1] = 0; before and after recurrence; Let J = next [I-1 ]; indicates that the first J of the first I-2 letter is the same as the last J; then if s [J] (J + 1) = s [I-1] (I letter), next [I] is equal to next [J] + 1; if not equal, then J into next [J ];
After reading two evening lessons, I can see it clearly .. It is really hard to understand...

The following is a small application: (poj2752, yzoi1780)
Given a string s with a length less than 400000, we need to find all I. The substring consisting of the first I letters that meet the requirements of S is the same as the string consisting of the last I letters;
All I incremental output is required;
For this question, you only need to find the next array. Be sure to find one more bit (calculated to the next [Len] position); j = Len; next [J] = T indicates that the first t letter of S is the same as the last t letter; then the second largest I (the maximum I is the length of the string) is obtained ); then, every cycle j = next [J]; The value of next [J] is an I value (remove 0 );

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.