Records the KMP algorithm, records its classic place

Source: Internet
Author: User

I have been away from school for many years and have ceased to fondle those old books.

Weekend, the weather in Shenzhen gloomy, God this time is always very happy to show off, to the Shenzhen people to come to the times of the rainstorm, it seems to be a year of rain all in these world-like.

So it's boring to stay at home and watch TV and surf the Internet. Casually opened the University of time (data structure, still keep Ah, the first time out of the total no confidence, always hope to be able to charge at any time). See the pattern matching chapter of the string. Suddenly found that the KMP algorithm is so classic. So remember ...

Before mentioning the classic of KMP, we should first mention the basic pattern matching algorithm:

The so-called pattern matching, simple point is to match two strings, to find out the position of a string in another string.

The basic pattern matching is this:

Suppose there is a string

S=s1s2 ... SN (due to the need for algorithmic efficiency, so generally put s[0] to save the length of s)

P=p1p2 ... PM (IBID.)

where (m<n), the position where p appears in S is required to be returned.

So the algorithm points are as follows:

Suppose that the scan starts at the I point in S (that is, starting from s[i], at this point with a variable k=i), in order to compare s[i] and p[1],s[i+1] and p[2]. In this way, when P goes to the J-character that is p[j], the s[i+j-1] and P[j] do not match, so it is necessary to return the pointer I of S to the next character from the beginning of the S, which is the k+1 continue to compare.

As shown in figure:

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.