Next array two kinds of method _ algorithm

Source: Internet
Author: User

I. Description
(1) See the same string on the Internet to find the next array of values are two kinds, one is the start of 1, one is the beginning of the 0, although there is a difference, but each of the next array of 0 starts with a corresponding entry in the next array that begins with-1, so it's more important to start with 1 or 0, so you can count The law is the same. KMP's original paper (k,m,p three guys) starts with 0, so the following is written in 0.
(2) on the next array of the method, the Internet can find a lot of popular concise writing, there are many articles on the concise code to explain very carefully, but this article is not the analysis of popular algorithms, but only to record their favorite calculation method, and the code to achieve.

Second, the text description of the method of seeking

(1) The first method: According to the next value of the previous character to find the string as P;next array of the record as next;

Convention: Subscript Starting from 1, note, not from 0 start counting string length >2

1 The next value of the first letter is set to 0 (next[1] = 0), and the next value of the second letter is placed 1 (next[2] = 1);
2 from the beginning of the 3rd, when the next value of the first position is computed, check

p[i-1]== P[next[i-1]]? (that is, whether the two values are equal)

Explanation: The value of the first position in the first position (that is, P[i-1], as M) is equal to the value (i.e. p[next[i-1]) of the next value of M (ie, next[i-1]) as N, (see the Meng Meng also does not matter, the following will have an example) if equal, then next[i] = Next[i-1] + 1

If not, then keep looking back, check

p[i-1]== P[next[next[i-1]]]? If equal, then next[i] = next[next[i-1]] + 1 if unequal, then continue to look back until you find subscript 1 (that is, the first element of the string), direct assignment next[i] = 1

(2) The second method: according to the maximum public element length
First of all, attach the address of the blog, which is explained in detail
http://blog.csdn.net/v_july_v/article/details/7041827

1) to calculate the maximum common element length of each letter prefix suffix
2 The maximum public element length moves one length backward, and the first element value fills 1, which is the initial version of the next array
3 (If you need the next array the first value is-1, this step can be omitted) the next array of each value of +1, that is, the next array is evaluated.

Iii. examples

String P = "Ababaaababaa"

Solving:
(1) corresponding to the first method of solving the above
1) Initialize P a B A B a A a B a B a a subscript 1 2 3 4 5 6 7 8 9 a next 0 1

2) to find the next value of the character labeled 3
P[3-1] = p[2] = ' B ';
NEXT[3-1] = next[2] = 1;
P[next[3-1]] = p[1] = ' a ';
P[3-1]!= P[next[3-1]], but this time it has traced back to the first element,
∴ Direct P[3] = 1; P a B A B a A a B a B a a subscript 1 2 3 4 5 6 7 8 9 The next 0 1 1

3) to find the next value of the character labeled 4
P[4-1] = p[3] = ' a ';
NEXT[4-1] = next[3] = 1;
P[next[4-1]] = p[1] = ' a ';
P[4-1] = = P[next[4-1]];
∴NEXT[4] = next[4-1] + 1 = 2; P a B A B a A a B a B a a subscript 1 2 3 4 5 6 7 8 9 each next 0 1 1 2

4) to find the next value of the character labeled 5
P[5-1] = p[4] = ' B ';
NEXT[5-1] = next[4] = 2;
P[next[5-1]] = p[2] = ' B ';
P[5-1] = = p[next[5-1]];
∴NEXT[5] = next[5-1] + 1 = 3; P a B A B a A a B a B a a subscript 1 2 3 4 5 6 7 8 9 One next 0 1 1 2

5) to find the next value of the character labeled 6
Derivation process Ibid => next[6] = next[6-1] + 1 = 4; P a B A B a A a B a B a a subscript 1 2 3 4 5 6 7 8 9 The next 0 1-1 2 3

6) to find the next value of the character labeled 7
P[7-1] = p[6] = ' a ';
NEXT[7-1] = next[6] = 4;
P[next[7-1]] = p[4] = ' B ';
P[7-1]!= P[next[7-1]] && is not back to the first one at this time, continue
Next[next[7-1]] = next[4] = 2;
P[next[next[7-1]] = p[2] = ' B '; (1)
P[7-1]!= P[next[next[7-1]] && but it's not back to the first one yet, go on
NEXT[NEXT[NEXT[7-1]]] = next[2] = 1;
P[NEXT[NEXT[NEXT[7-1]] [] = p[1] = ' a ';
P[7-1] = = p[next[next[next[7-1]]];
∴NEXT[7-1] = next[next[next[7-1]]] + 1 = next[2] + 1 = 2; P a B A a b a a a B a B a A a subscript 1 2 3 4 5 6 7 8 9 The next 0 1 is 1 2 3 4

7) To find the next value of the character labeled 8
P[8-1] = p[7] = ' a ';
Next[8-1] = next[7] = 2;
P[next[8-1]] = p[2] = ' B ';
P[8-1]!= P[next[8-1]], but not yet back to the first element, continue
Next[next[8-1]] = next[2] = 1;
P[next[next[8-1]]] = p[1] = ' a ';
P[8-1] = = P[next[next[8-1]];
∴NEXT[8] = Next[next[8-1]] + 1 = 2 P A b A B a a B a B a a b. a the subscript 1 2 3 4 5 6 7 8 9 The next 0 1 1 2 3 4 2-2

8) To find the next value of the character labeled 9
Derivation process with 4) => next[9] = next[9-1] + 1 = 3; P a B A a b a a a B a B a A a subscript 1 2 3 4 5 6 7 8 9 The next 0 1 the 1 2 3 4 the 2 2

9) To find the next value of the character labeled 10
Derivation process with 4) => next[10] = next[10-1] + 1 = 4; P a B A a b a a a B a B a A a subscript 1 2 3 4 5 6 7 8 9 The next 0 1 the 1 2 3 4 2 2 3

10) to find the next value of the character labeled 11
Derivation process with 4) => next[11] = next[11-1] + 1 = 5; P a B A a b a a a B a B a A a subscript 1 2 3 4 5 6 7 8 9 The next 0 1 the 1 2 3 4 2 2 3 4

11) To find the next value of the character labeled 12
Derivation process with 4) => next[12] = next[12-1] + 1 = 6; P a B A a b a a a B a B a A a subscript 1 2 3 4 5 6 7 8 9 The next 0 1-1 2 3 4 2 2 3 4 5 6

(2) corresponding to the second method of solving the above
1) to calculate the maximum common substring length of each letter prefix suffix (the next step is to remove the last one, so the last one can not be counted) (2) P a B A B a a B a a A and a "a" the prefix maximum common substring length 0 0 1 2 3 1 1 2 3 4 5

2 Maximum common substring length the whole is moved backward by one length, and the first element value is filled with-1, that is, the initial version of the next array P a B a a B a a a a B a next array first edition-1 0 0 1 2 3 1 1 2 3 4 5

3 (If you need the next array the first value is-1, this step can be omitted) the next array of each value of +1, that is, the next array is evaluated. P a B A B a A a B a B a a next array second Edition 0 1 1 2 3 4 2 2 3 4 5 6

Four, code implementation

(1) corresponding to the first of the above method

#include <iostream> #include <vector> #include <string.h> using namespace std;
        Class solution{public:vector<int> getNext2 (string ps) {vector<int> next;
        int L = (int) ps.length ();
        if (L = = 0) {return next;
            }else if (L = = 1) {next.push_back (0);
        return to Next;
            }else if (L = = 2) {next.push_back (0);
            Next.push_back (1);
        return to Next;
        } Char p[20];
        strcpy (P, ps.c_str ());//string-turn character array next.push_back (0);
        Next.push_back (1);
            for (int i = 2;i< (int) ps.length (); i++) {int k = next[i-1];
                    while (k!=0) {if (p[i-1] = = P[k-1]) {//k-1 is because, in the computer, the array subscript is the starting 0 next.push_back (k+1);
                Break
                }else{k = next[k-1];
            } if (k==0) {next.push_back (1);
    }    return to Next; }
};

(2) corresponds to the second method above

#include <iostream> #include <vector> #include <string.h> using namespace std;
        Class solution{public:vector<int> getNext2 (string ps) {vector<int> next;
        int L = (int) ps.length ();
        if (L = = 0) {return next;
            }else if (L = = 1) {next.push_back (0);
        return to Next; Next.push_back (0);//next array The first value is 0 for (int i = 0; i < l-1; i++) {//Last maximum common substring length do not count//calculate each child
            Prefix suffix of string maximum common substring length int l = MAX_PUB_SUBSTR (Ps.substr (0,i+1)); After the right shift +1//for cycle has been added a data 0 into next array, when added to the element, the element in the next array in the subscript than the last character of the element value of the substring of the final characters of the subscript 1, it is equivalent to move backwards a next.push_
        Back (l+1);
    return to Next;
        ///compute the maximum common substring length of the prefix suffix int max_pub_substr (string ps) {int l = (int) ps.length ();
        if (L = = 0 | | | = = = 1) {return 0;
        } Char p[20];
        strcpy (P, ps.c_str ());//string-character array int len = 0; int m = -1;//LastA character (not included) that is equal to the last character Poute m int k = -1;//word that has been found Poute while (k!= l-1) {k = m+1;
                    for (; k < l-1; k++) {if (p[k] = = P[l-1]) {m = k;
                Break
            } if (M==-1 | | k==l+1) {//indicates that there is no character equal to the last character return Len;
                }else{//checks whether the prefix string and suffix string are equal int i = 0,J = L-1-m;
                        for (; I <= m,j < L; i++,j++) {//i prefix subscript, j suffix subscript//As long as there are unequal on failure if (P[i]!= p[j]) {
                    Break
                } if (i = = m+1) {//Description The string is equal, as all comparisons are made without interrupting len = m+1;
    }} return len; }
};

V. Verification

int main () {
    string s = ' Ababaaababaa ';
    Solution SLT;
    Vector<int> next = slt.getnext2 (s);
    Vector<int>::iterator it;
    for (it = Next.begin (); it!= next.end (); it++) {
        cout<<*it;
    }
    return 0;
}

Vi. External
(1) In this place, we can find that p[2] = = p[4] = = ' B ', due to p[4]!= p[6], ∴p[2]!= p[6] is certain, and can be skipped p[2] and p[6] comparison, direct comparison p[1] and p[6];
(2) The maximum common element length of the prefix suffix

Prefix: In simple terms, that is, a substring beginning with the first letter of a string from the first letter (which must be included) to see the last letter (excluding).
(For example, "abab" prefix has A,ab,aba);

Suffix: In short, that is, from the last letter (must include) to start to see the first letter (excluding) the substring of the string
(For example, "abab" suffix has b,ab,bab);

Maximum common substring length: the maximum length of the same substring owned by the prefix and suffix
Take "Abab" as an example: the maximum public element length of each substring prefix suffix of a pattern string a null 0 ab a b 0 ABA a,ab a,ba 1 abab a,ab,aba b,ab,bab 2

A slightly faster way to do this:
The common substring of the prefix "ABAB" must begin with a (the first letter of the string), B (the last letter of the string), the substring that satisfies the condition in the prefix string of "Abab" is a={"AB"}, and the substring of the suffix string satisfies the condition of the b={"AB"}, and then find A, A set of equal substrings in the B set C, and the length of the oldest string in C is the maximum common substring length.

Well ~ ~ ~ So much has been written, I am too lazy to look at t_t

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.