Basic operation and self-understanding of the KMP algorithm

Source: Internet
Author: User
Tags first string string back

1.1 KMP needed to solve the problem

KMP, is the sum of three names. The main problem is the string matching problem. That is to give a certain length of strings and a text, asking you to find the string in the text where the first occurrence, and then the string and text length may be very large.

As such a question: POJ-3461

Test instructions is probably to find out the number of occurrences of the first string in the second string.

1.2 Traditional brute force algorithms

The brute force algorithm is to search by brute force, starting with the first letter. If it does not match, then move the entire string back one bit. The algorithm (on^2) obviously becomes unbearable in some cases. But we can find out by understanding The main reason for the slowness of this algorithm is that when a letter mismatch is found, it is only possible to move back one bit. Then KMP is the way to optimize this backward jump. To achieve the goal of optimizing time .

The specific operation process of 1.3 KMP algorithm

Let's not talk about the principle of this algorithm, because I am the first to know the operation process, and then the general understanding.

1.3.1 Longest prefix string

We introduce a concept, the longest prefix string

Some strings like this:

Abccsab its longest prefix string is AB

ABSSC It does not have the longest prefix string

AAAAA its longest prefix string is AAAA

1.3.2 Next Array

This is the first step we need to take. Divide this string into

Basic operation and self-understanding of the KMP algorithm

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.