KMP (Knuth–morris–pratt) algorithm

Source: Internet
Author: User

Article string S, string W to look for

Focus: Get int t[] from W

Use the variable c to represent the preceding character in the W that is currently being compared.
I represents the index in T currently being processed,
int[] T = new Int[w.length];
T[0] =-1; T[1] = 0;
A=r; i=2

Example
Array:a b A B c d a b a B a B
Idx:0 1 2 3 4 5 6 7 8 9 10 11

Case 1:w[c] = = w[i-1]--t[i] = ++c; i++;
Case 2:w[c]! = w[i-1] && c>0--C = t[c];
Case 3:w[c]! = w[i-1] && c==0 to t[i] = 0; i++;

Procedure
array:a    b    a    b    c    d    a    B    a    b    a    b
idx    : 0    1    2    3    4    5    6    7    8    9    11

I 2 3 4 5 5 6 7 8 9 10 11
Case 3 1 1 2 3 3 1 1 1 1 2
Pre-c: 0 0 1 2 0 0 0 1 2 3 4
After-c: 0 1 2 0 0 0 1 2 3 4 2
Comparison: 0 & 0 0 & 2 1 & 3 2 & 4 0 & 4 0 & 5 0 & 6 1 & 7 2 & 8 3 & 9 4 & 10

The last Po section of his own written leetcode on the implement STRSTR () code

1  Public classSolution {2      Public intstrStr (String haystack, string needle) {3         if(needle==NULL|| Needle.length () ==0)4             return0;5         intNdlen =needle.length ();6         intHslen =haystack.length ();7 8         if(ndlen==1) {9             intI=0;Ten             Charc = Needle.charat (0); One              while(i<Hslen) { A                 if(Haystack.charat (i) = =c) { -                     returni; -                 } thei++; -             } -             return-1; -         } +  -         int[] T =New int[Ndlen]; +T[0] = 1; AT[1] = 0; at         inti=2, Cnd=0; -          while(i<Ndlen) {  -             if(Needle.charat (CND) ==needle.charat (i-1)) {  -cnd++;  -t[i++] =CND; -}Else if(cnd>0) { inCND =T[CND]; -}Else { tot[i++] = 0; +             } -         } the  *         intNdidx=0, hsidx=0; $          while(hsidx<Hslen) {Panax Notoginseng             if(Haystack.charat (hsidx) = =Needle.charat (NDIDX)) { -hsidx++; thendidx++; +                 if(Ndidx = =Ndlen) { A                     returnhsidx-Ndlen; the                 } +}Else if(T[ndidx]!=-1) { -Ndidx =T[ndidx]; $}Else { $hsidx++; -             } -         } the         return-1; -     }Wuyi}

KMP (Knuth–morris–pratt) 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.