KMP improvement (nextval)

Source: Internet
Author: User

Why should we improve next? Because next has a defect... O (& cap; _ & cap;) O haha ~...

What are the defects?

The example in the instructor's PPT in the class is not very good and does not reflect the core. It is only a part of it. Therefore, please take a closer look at the courseware and the example below.

Set S string abcabdabcabcd; P string abcabcd

Okay, first find the next [] of the P string. (obedient, find next []. Don't look at it. Let's ask you to find next on your computer. No, code in the previous blog (http://www.cnblogs.com/xiaoshanshan/p/4081693.html ));

 

Well, you're probably not obedient. Forget it. Let's take a look.

Next [0 .. 6] = {-, 3 };

Next we will focus on it. Let's take a look at how KMP is implemented (KMP calls you, come out) (slag )... O (& cap; _ & cap;) O haha ~...

This process is a bit long. Please take a look at it. (it takes a longer time for me to launch ....)

S a (bcabdabcabcd) AB (cabdabcabcd) abc (abdabcabcd) abca (bdabcabcd) abcab (dabcabcd) abcabd (abcabcd)

P a (bcabcd) AB (cabcd) abc (abcd) abca (bcd) abcab (cd) abcabc (d)

(I wipe... Look at my next...Next [5] = 2)

S abcabd (abcabcd)

P abc (abcd)

(KMP, you can stop .) (Slag ).... O (& cap; _ & cap;) O haha ~...

 

You can see it. What are you looking? What do you mean by reading the above? Let's look down:

Just now P [next [5] = P [5] = 'C', right. Okay, now I understand. This match is white match, because it certainly does not match. Now you understand.

Nana? You still don't understand, P [5] = P [2], right, P [5] is not good, P [2] is definitely not good. (Next, call nextval)

 

So before I talk about nextval, let's take a look at how nextval works.

S a (bcabdabcabcd) AB (cabdabcabcd) abc (abdabcabcd) abca (bdabcabcd) abcab (dabcabcd) abcabd (abcabcd)

P a (bcabcd) AB (cabcd) abc (abcd) abca (bcd) abcab (cd) abcabc (d)

(Ah !! Check my nextval ...)

 

S abcabd (abcabcd)

P a (bcabcd)(Key part)

(Nana? I will try again ...)

 

S abcabda (bcabcd)

P a (bcabcd) (I will not write it next, and finally match it)

 

Here, please pay attention to (key part). How is it? Is nextval a little better than next (please refer to their differences ).

Okay, I don't want to talk much about it. Let's go to the code.

1 void Get_next (string P, int * nextval) {2 int I, j; 3 nextval [I = 0] = j =-1; 4 int len = P. length (); 5 while (I <len-1) {6 if (j =-1 | P [I] = P [j]) {7 I ++, j ++; 8 if (P [I] = P [j]) {9 nextval [I] = nextval [j]; 10} 11 else nextval [I] = j; 12} 13 else j = nextval [j]; 14} 15}

 

It's so simple.

Exercise, what is the nextval of the P string abcabcd?

Nextval [0 .. 7] = {-, 0 };

 

KMP improvement (nextval)

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.