KMP Algorithm Summary

Source: Internet
Author: User

The KMP algorithm is a basic string matching algorithm, but some of the details of the code implementation are error-prone. This essay will be carefully summed up.

The core of the KMP algorithm is:

The KMP algorithm searches for occurrences of a "word" W within a main "text string" S By employing the observation if a mismatch occurs, the word itself embodies sufficient information to dete Rmine where the next match could begin, thus bypassing re-examination of previously matched characters. (Form Wikipedia)

First define a concept

Given the string s[0..l-1], if there is a regular prefix of s (proper prefix) s[0..i] (I<L-1) is also the suffix of s, then the prefix is called the key (critical prefix).

Obviously an empty string is a key prefix for any non-empty string.

For pattern string W, preprocess An array of length |w| next[0..| W|-1],nt[i] The length of the longest key prefix of w[0..i] that represents the prefix of W.

With next[] arrays, you can use the O (|s|) Complete the search within the time.

Specific implementation and complexity analysis skipped, leaving K. M. P. Links to three-person papers

Knuth, Donald; Morris, James H.; Pratt, Vaughan (1977). "Fast pattern matching in strings". SIAM Journal on Computing 6 (2): 323–350.doi:10.1137/0206024.

------------------------------------------------------------

Title Link: Hihocoder 1015

#include <bits/stdc++.h>using namespacestd;Const intN (1e4+5), M (1e6+5);CharS[n], t[m];intNt[n];intMain () {intN; scanf ("%d", &N);  for(intLS, K, ans;n--;) {scanf ("%s%s", S, t); K=nt[0]=0;  for(inti=ls=1; S[i]; i++, ls++){             for(; k&&s[k]!=s[i];) k=Nt[k]; Nt[i]=s[i]==s[k]?++k:k; } ans=k=0;  for(intI=0; S[n]; i++){            //K:t[0..i-1] The matching length             for(; K&&s[k]!=t[i];) k=nt[k-1];//Error-prone            if(t[i]==S[k]) {k++; if(K==ls) ans++; }} printf ("%d\n", ans); }}

Two of the comments in the code are easy to write wrong, and the typical error is

 for (; k&&s[k]!=s[i];) k=nt[k];
For

This error pit in: Often can be over the sample, after submission will not WA but will tle.

----------------------

Next[i] can also be defined as the length of the longest key prefix of the prefix w[0..i] minus one, at which point the meaning of next[i] can be changed to the end of the longest critical prefix of the prefix w[0..i].

The code has to be changed slightly.

#include <bits/stdc++.h>using namespacestd;Const intmax_n=1e6+Ten;CharS[max_n], t[max_n];intNt[max_n];voidGet_next (Char*s) {nt[0]=-1; intk=-1;  for(intI=1; S[i]; i++){         while(k!=-1&&s[k+1]!=s[i]) k=Nt[k]; if(s[k+1]==s[i]) k++; Nt[i]=K; }}intans;voidMatchChar*s,Char*t) {    intLs=strlen (s), k=-1;  for(intI=0; S[n]; i++){         while(k!=-1&&s[k+1]!=t[i]) k=Nt[k]; if(s[k+1]==t[i]) k++; if(k==ls-1) ans++; }}intMain () {intN; scanf ("%d", &N);  while(n--) {scanf ("%s%s", S, t);        Get_next (s); Ans=0;        Match (S, T); printf ("%d\n", ans); }    return 0;}

KMP Algorithm Summary

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.