Introduction to the KMP algorithm of "first knowledge"

Source: Internet
Author: User

As an example,

Pattern string S:a s d a s d a S d f a s D

Matched string T:a s d a s D F

If you use the naïve matching algorithm--

1 2 3 4 5 6 7 8 9

A s d a s D a s d f a s D

A s d a s D f

1 2 3 4 5 6 7

At this point, match to S7 and T7, S7 A and T7 for F, mismatch so simple matching algorithm will do this-

1 2 3 4 5 6 7 8 9

A s d a s d a S d f a s D

a s d a s D F

1 2 3 4 5 6 7

At this point, we will find that the pattern string goes back to S2, and the matching string goes back to T1.

Obviously, this will greatly reduce the efficiency of the algorithm, in the worst case, we need to query the pattern string almost every element, and each query from the string of matching string to close to the end of the string, such a time complexity of n*m, where N and M are the pattern string and the length of the matching string.

So is it possible to reduce the complexity of our time? The answer is yes-obviously we only need to find a way to reduce the backtracking, we can achieve the effect. The KMP algorithm uses this method to save time.

1 2 3 4 5 6 7 8 9

A s d a s D a s d f a s D

A s d a s D f

1 2 3 4 5 6 7

Is this thing familiar? Just appeared once.

So how does the KMP algorithm perform the next step? The answer is as follows--

1 2 3 4 5 6 7 8 9

A s d a s D a s d f a s D

A s D a s D F

1 2 3 4 5 6 7

Watch this step! The pattern string here does not backtrack at all, but moves the matching string backwards a few steps. This way, the worst case is simply to go through the pattern string, and then the matching string to go through, of course, the matching string inside the elements will go many times, but, it is obvious that the algorithm will reduce the n*m to N+k, this k and m internal parts of the repetition of the number of elements, the maximum will not exceed N (of course, this is Not necessarily right, I will continue to prove it later).

Well, the method knows, so how to achieve it?

In other words, how do you implement a fast-moving matching string? The answer is--add a next array that marks the attributes in the matching string.

This next array has obvious characteristics

    1. Next[0] =-1, that is, this is the first element, and there is no one in front to replace it.
    2. NEXT[J] = k; {k | T[0] = T[j-k], t[1] = t[j-k+1],..., t[k-1] = t[j-1]}.
    3. NEXT[J] = 0; Other circumstances.

Example:

Matched string T:a s d a s D F

Next:-1 0 0 0 1 2 3

See the code in detail--

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <algorithm>6 using namespacestd;7 8 Const intN = .;9 Ten CharS[n], t[n]; One intNext[n]; A intLenS, LenT; -  - voidKmpnext (CharT//Calculate next Array the { -     inti =1; -next[0] = -1;//next[0] = 1 -      while(I <LenT) +     { -         intj =0; +          while(T[j] = = T[i])//Next[i] = j; {J | T[0] = T[i-j], t[1] = t[i-j+1],..., t[j-1] = t[i-1]} A         { atNext[i] =J; -i++; -J + +; -         } -Next[i] = j;//ibid., or equal to 0 -i++; in     } - } to  + BOOLKmpChar) p,CharT//KMP - { theLenS =strlen (S); *LenT =strlen (T); $ Kmpnext (T);Panax Notoginseng     inti =0, j =0; -      while(I < LenS && J < LenT)//exit when pattern string or match string is gone the     { +         if(j = =-1) A         { thei++; +j =0; -         } $         Else if(S[i] = =T[j]) $         { -i++; -J + +; the         } -         Elsej =Next[j];Wuyi     } the     if(j = = LenT)return 1;//if the matching string is finished, the substring of the pattern string is indicated -     return 0; Wu } -  About intMain () $ { -     //freopen ("test.in", "R", stdin); -      while(~SCANF ("%s%s", S, t)) -     { A         if(KMP (S, t)) printf ("yes\n"); +         Elseprintf"no\n"); the     } -     return 0; $}
View Code

Introduction to the KMP algorithm of "first knowledge"

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.