Next[] Array in the KMP algorithm

Source: Internet
Author: User

The KMP algorithm is the most difficult to understand is the next[] array of the method.

As an example, the following is a substring of the next array of values, you can see that this substring is very symmetrical, so the next value is relatively large.

Position I

0

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

Prefix Next[i]

0

0

0

0

1

2

3

1

2

3

4

5

6

7

4

0

Sub-string

A

G

C

T

A

G

C

A

G

C

T

A

G

C

T

G


Statement: The following symmetry is not a central symmetry, but the central word converts sequential blocks symmetry, such as not ABCCBA, but abcabc this symmetry.


(1) Find the symmetric string one by one.
This is very simple, we just loop through the substring, see the first 1 characters, the first 2 characters, 3 ... I last to 15.


1th A is asymmetric, so the symmetry degree 0


The first two AG is asymmetric, so is also 0


The first 0-4 is the same 0.


The first 5 agcta, you can see that the string has an equal, so the degree of symmetry is 1 the first 6 Agctag, see AG and AG pair, symmetry degree of 2


Here to pay attention, want to think so, programming how to achieve it?


Just follow the rules below:
A, when the previous character of the current polygon has a symmetry of 0, simply compare the current character to the first character of the substring. This is very good understanding ah, the front is 0, the explanation is not symmetrical, if added a character, to symmetry is the most current and the first symmetry. For example agcta this inside T is 0, then the symmetry of a behind only need to see it is not equal to the first character A.





B, according to this reasoning, we can summarize a rule, not only the front is 0 ah, if the previous character of the next value is 1, then we will be the current character and substring of the second character to compare, because the preceding is 1, stating that the preceding character is equal to the first, if this is equal to the second one, It shows that symmetry is 2. There are two characters symmetrical. For example above Agctag, the next of the penultimate A is 1, indicating that it is symmetrical with the first a, then we will compare the last G with the second G, and equal, the natural symmetry of Chengdu is cumulative, is 2.





C, according to the above reasoning, if it has been equal, has been cumulative, you can always push ah, push to here should be a bit difficult to have it, if you think it is difficult to explain that I write too failed.


Of course, it's not going to be so smooth. Let us always symmetry, if we encounter the next unequal, then the description can not inherit the preceding symmetry, this situation can only show that there is not so much symmetry, but can not be explained that a little symmetry is not, so it is necessary to reconsider this situation, this is also the difficulty.
(2) Looking back for symmetry
Here can not inherit the front, but still look for symmetry in Chengdu, the most foolish thing to write a sub-function, find the maximum symmetry of the string, how to write a lot of it, such as find out all the current string, and then go forward to see whether it is equal, and finally go to the beginning of the substring, of course, this is the stupidest The KMP we generally see are optimized because the strings are regular.


Here is an example of a paragraph from the table above:


Position i=0 to 14 as follows, I add parentheses just to illustrate the problem:


(A G c t a G C) (A G c t a G C) t


We can see this, the final symmetry before this T is: 1,2,3,4,5,6,7, the second-to-last C looks forward with a 7-character symmetry, so called 7. But at the end of this t does not inherit the previous symmetry degree next value, so the symmetry of this t is to seek again.


There are several facts to be stated here first.
1, T if there is symmetry, then the degree of symmetry is certainly more than the symmetry of the previous C is small, so to find a smaller symmetry, this does not need to explain it, if the big so T inherits the symmetry of the front.


2, in order to find a smaller symmetry, there must be a symmetry inside there is also a sub-symmetry, and this t has to be immediately after the sub-symmetry.

Attached code

#include <iostream> #include <algorithm> #include <vector> #include <string.h> #include < Ctype.h> #include <math.h>using namespace std;void fun (), int main () {fun (); return 0;} void Fun () {int I,j,nextarr[1000];char str1[1000];gets (str1); Nextarr[0]=-1;j=-1;i=0;while (i < strlen (str1)-1) {if ( j = =-1 | | Str1[i] = = Str1[j]) {i++;j++;nextarr[i] = j;} Else{j = Nextarr[j];}} For (I=1;i<strlen (STR1); i++) cout<<nextarr[i]<< "";}





Next[] Array in 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.