Kmp-leetcode #459 repeated Substring Pattern

Source: Internet
Author: User

Review the KMP algorithm

The main idea of KMP is to construct the next array by using the symmetry of the prefix suffix of the string itself to achieve a string matching with a time complexity of near O (N)

For a string str,next[j] = k to satisfy str[0...k-1] = Str[j-k...j-1] The largest k, that is, for the substring str[0...j-1], the first k letters equal to the after K letters

Now solve the next array of str:

Initialize: next[0] = 1

So, in the case of knowing Next[j], how do you get the next[j+1 by hand? In two cases (make k=next[j]):

1, if str[j]==str[k], then next[j+1] = k+1

As shown, for Str[0...j-1], the first k letters are equal to the last K letters (two green parts are equal), and then Str[k] is just the next letter of the first K letter (red One)

If STR[J]==STR[K], description for STR[0...J], the first k+1 letter equals the k+1 letter (green + red = green + red), which equals next[j]+1 (green length is k, red length is 1)

2, if Str[j]!=str[k], then k=next[k], and then continue the cycle (back to 1), until K=-1

Because Str[j]!=str[k] (medium purple and red are not equal), so the first k+1 letter is no longer equal to the k+1 letter.

But since the first K-letters are equal to the latter K-letters (two black dashed lines in the figure), so for any K ' <k,str[k-k ' ... k-1]=str[j-k ' ... j-1] (second and last green equal in the figure)

While Next[k] represents the symmetry inside the str[0...k-1], so that K ' =next[k], then for Str[0...k-1], the first K ' letter equals the after K ' letter (the first and second green equal in the figure)

Since the second green in the figure is always = Fourth green, the first green is equal to the fourth green

So K=next[l] continues to be brought into the loop, returning to judgment 1:

If Str[k ']=str[j], then satisfies the former K ' + 1 letters equals after K ' + 1 letters (two light yellow areas equal), so next[j+1] = K ' +1;

Otherwise, continue the K ' =next[k '] loop until the K ' =-1 description has reached the first element, cannot continue to divide, next[j+1]=0

 

After getting the recursive method of the next array, it is now implemented in C + +

void GetNext (String str, int next[]) {int len = Str.length (); next[0] = -1;int j = 0, k = -1;while (j<len-1) {if (k==-1 || STR[J]==STR[K]) Next[++j] = ++k;elsek = Next[k];}}

Here's an explanation: because after each round of assignment next[j], K is otherwise-1, otherwise next[j] (the next position of the last matching prefix)

If k=-1, explain next[j+1]=0=k+1, otherwise if STR[J]==STR[K], the former k+1 letter equals the k+1 letter, directly next[j+1]=k+1

So the statement after the IF in the loop is "next[++j" = ++k;

Now solve the problem on Leetcode with the idea of solving next array

https://leetcode.com/problems/repeated-substring-pattern/

Given a non-empty string check if it can be constructed by taking a substring of it and appending multiple copies of the S Ubstring together. Assume the given string consists of lowercase Chinese letters only and its length would not exceed 10000

Example:

Input: "Abcabcabcabc" Output:TrueExplanation:It ' s The substring "abc" four times. (and the substring "ABCABC" twice.)

Assuming that the STR length is Len and the repeating substring length is k, then if it is true that a string of multiple consecutive lengths of K is repeated to form STR, then when the next is obtained for STR, the continuous symmetry (the equal of the strings in the front and back of the two dashed boxes) will start from next[k+1]. Increment , until Next[len]=len-k, and (len-k)%k==0, indicating an integral number of K

Always ask for Next[len] instead of next[len-1], because next[len-1] simply represents the inner symmetry of the first len-1 letters, without taking into account the last letter that str[len-1]

So the solution is very simple: first the next array of STR, always find Next[len], and then see if Next[len] is not 0 and divisible by K (K=len-next[len])

BOOL Repeatedsubstringpattern (string str) {int len = str.length (); int next[len+1];next[0] = -1;int j = 0, k = -1;while (j& Lt;len) {if (K==-1 | | str[j]==str[k]) NEXT[++J] = ++k;else k = next[k];} Return next[len]&&next[len]% (Len-next[len]) ==0;}

Time complexity is only O (N), and violence requires O (n^2)

Kmp-leetcode #459 repeated Substring Pattern

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.