KMP Algorithm Summary

Source: Internet
Author: User


KMP algorithm, which is a pattern matching algorithm proposed by Knuth,morris,pratt, is a very good pattern matching algorithm for any pattern and target sequence, it can complete matching search in linear time without degradation.

/** next[] Meaning (premise): x[i-next[i]...i-1] = X[[i]-1] This is important, * next[i] to meet the maximum value of x[i-z...i-1] = X[0...z-1] Z (is the self-matching of x); /c1>*/  //the code implementation of next /** next[] [next[m], this next[m] role is still very large;*/voidKmp_pre (CharX[],intMintnext[]) {    intI, J; J= next[0] = -1; I=0;  while(I <m) { while(-1! = J && X[i]! = x[j]) j =Next[j]; //j =-1, indicating that the No. 0 digit did not match successfully; it is necessary to advance one directly;Next[++i] = + +K; }}/** There can also be a small optimization;*/voidPREKMP (CharX[],intMintkmpnext[]) {    intI, J; J= kmpnext[0] = -1; I=0;  while(I <m) { while(-1! = J && X[i]! = x[j]) j =Kmpnext[j]; if(X[++i] = = X[++j]) Kmpnext[i] =Kmpnext[j]; ElseKmpnext[i] =J; /*this if is 6, which removes some meaningless next[], presumably meaning * if the x[j] match fails, then execute j = next[j];        * and x[j] = x[next[j]] [so x[next[j]] will certainly match the failure.        * So it is meaningless to say this next[j]. */    }}/**x and y match; * Returns the number of occurrences of x in Y, which can overlap * and the next[] function is basically similar to the wording;*/  intnext[10010]; intKmp_count (CharX[],intMCharY[],intN) {//x is the pattern string, and Y is the main string;     intI, J; intAns =0; //PREKMP (x, M, next);kmp_pre (x, M, next); I= j =0;  while(I <N) { while(-1! = J && Y[i]! = x[j]) j =Next[j]; I++; J + +; if(J >=m) {ans++; J=Next[j]; }     }     returnans;}

Classic topics:

Read his blog: plus a title:
/** Pattern string can be floating pattern string matching problem * Given the relative size of the pattern string, need to find out the pattern string match number and position * such as pattern string: 1,4,4,2,3,1 and Main string: 5,6,2,10,10,7,3,2,9 * Then substring: 2,10,10,7,3, 2 is matched to the pattern string. * Idea: Just compare the number of the number with the current number is smaller than the current number is good, see whether these two things are equal to carry out KMP. */ //#include <bits/stdc++.h>#include <iostream>#include<string>#include<queue>#include<map>#include<cstring>#include<cstdio>#include<vector>using namespaceStd;typedefLong LongLL;#defineLson K<<1, LL, mid#defineRson k<<1|1, mid+1, RRConst intMAXN =100008;intN, K, S, next[maxn>>2], as[MAXN] [ -], bs[maxn>>2][ -], A[MAXN], B[maxn];vector<int>ans;voidInit () {//assimilation of input strings into as and BS;scanf"%d%d%d", &n, &k, &s);  for(inti =0; I < n; i++) {scanf ("%d", &A[i]); if(I! =0) {             for(intj =1; J < -; J + +)                 asI [j] = as[I1][j]; }         asI [a[i]]++; }     for(inti =0; I < K; i++) {scanf ("%d", &B[i]); if(I! =0) {             for(intj =1; J < -; J + +) Bs[i][j]= bs[i-1][j]; } Bs[i][b[i]]++; }}//here is the same way of writing without nested while loops. voidBuild_next () {next[0] = -1; next[1] =0;//There's a skin here.    intj =0, i =1;  while(I <k) {intT11 =0, T12, t21 =0, t22;  for(intt =1; T < B[i]; t++)            if(i = = j) T11 + =Bs[i][t]; ElseT11 + = (bs[i][t]-bs[i-j-1][t]); if(i = = j) T12 =Bs[i][b[i]]; ElseT12 = bs[i][b[i]]-bs[i-j-1][b[i]];  for(intt =1; T < B[j]; t++) t21+=Bs[j][t]; T22=Bs[j][b[j]]; if(T11 = = T21 && T12 = =t22) next[++i] = + +J; Elsej =Next[j]; }}voidKMP () {ans.clear ();    Build_next (); inti =0, j =0;  while(I <N) {intT11 =0, T12, t21 =0, t22;  for(intt =1; T < A[i]; t++)            if(i = = j) T11 + = as[i][t]; ElseT11 + = ( asI [t]- as[i-j-1][t]); if(i = = j) T12 = as[I][a[i]]; ElseT12 = asI [a[i]]- as[i-j-1][a[i]];  for(intt =1; T < B[j]; t++) t21+=Bs[j][t]; T22=Bs[j][b[j]]; if(T11 = = T21 && T12 = =t22) {            ++i; ++J; if(J >=k) {Ans.push_back (i-j+1); J=Next[j]; }        }        Elsej =Next[j]; }}intMain () {//freopen ("In.txt", "R", stdin);init ();    KMP (); printf ("%d\n", S =ans.size ());  for(inti =0; I < S; i++) printf ("%d\n", Ans[i]); 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: 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.