Thoroughly understand baked bun slice algorithm

Source: Internet
Author: User

KMP also known as roasted (k) bun (m) slice (p) algorithm, in fact, through the characteristics of the pattern string itself to optimize the pattern string matching

NEXT[J] indicates that the string preceded by J has the same prefix suffix of length next[j]

Move position at mismatch = location of mismatch character-next value corresponding to mismatch character, i.e. J-next[j]

Next[0]=-1 means to move the string 1 bits to the right when the 0-bit mismatch is placed

1. Find the next array:

NEXT[J] indicates that the string preceded by J has the same prefix suffix of length next[j]

The process of finding the next array equals the pattern string itself and matches itself

void GetNext () {    int  i,j;    I=0; j=-1; next[0]=-1;      while (i<Tlen)    {        if(j==-1| | T[I]==T[J]) next[++i]=++J;         else j=next[j];}    }

2. Use the next array to find the minimum cycle section length:

If a string can consist of K-strings, the minimum loop section of the string is the substring, and the cycle time is K.

such as the ABCD Minimum cycle section is the ABCD,ABCABC minimum cycle section is ABC

How to find the minimum circulation section? such as Abcabcabc

The next array is:

A B C A B C A B C   
0 1 2 3 4 5 6 7 8 9
-1 0 0 0 1 2 3 4 5 6

Next[len] is a very special value that represents the same maximum length of the entire string prefix

such as Shangli son, next[len]=6

A[0]-A[5] In the description string is exactly the same as the a[3]-a[8]

That is a[0]-a[2] and a[3]-a[5] are exactly the same, a[3]-a[5] and a[6]-a[8] are exactly the same,

That is, the string can be made up of a[0]-a[2] 3 cycles.

So as long as tlen% (Tlen-next[tlen]) ==0&&next[tlen]!=0 string can be made up of substrings smaller than its length

Otherwise the Loop section is for it itself

 while (~SCANF ("%s", T)) {    Tlen=strlen (t);    GetNext ();     if (tlen% (Tlen-next[tlen]) = =0&&next[tlen]!=0) printf ("%d\n ", tlen-Next[tlen]);     Else printf ("%d\n", Tlen);}

3.KMP where to find the first occurrence of the pattern string

intKmp_index () {intI=0, j=0;    GetNext ();  while(i<slen&&j<Tlen) {        if(j==-1|| s[i]==T[j]) {i++; J++; }        Elsej=Next[j]; }    if(J==tlen)returnI-Tlen; Else return-1;}

4.KMP the number of occurrences of a pattern string in a matching string (overlapping and non-overlapping points)

intKmp_count () {intans=0; inti,j=0; if(slen==1&&tlen==1)    {        if(s[0]==t[0])return 1; Else return 0;    } getNext ();  for(i=0; i<slen;i++)    {         while(j>0&AMP;&AMP;S[I]!=T[J]) j=Next[j]; if(S[i]==t[j]) J + +; if(j==Tlen) {ans++; J=0; //if the substring can overlap j=next[j];        }    }    returnans;}

Roasted (k) steamed bun (m) tablets (p) full Template:

#include <cstdio>#include<cstdlib>#include<cstring>#include<iostream>#defineN 1000010using  namespacestd;intNext[n];CharS[n],t[n];intSlen,tlen;voidGetNext () {intj,k; J=0; k=-1; next[0]=-1;  while(j<Tlen) {        if(k==-1|| T[J]==T[K]) next[++j]=++K; Elsek=Next[k]; }}//returns the position of the first occurrence of the pattern string T in the main string sintKmp_index () {intI=0, j=0;    GetNext ();  while(i<slen&&j<Tlen) {        if(j==-1|| s[i]==T[j]) {i++; J++; }        Elsej=Next[j]; }    if(J==tlen)returnI-Tlen; Else return-1;}//returns the number of occurrences of the pattern string T in the main string sintKmp_count () {intans=0; inti,j=0; if(slen==1&&tlen==1)    {        if(s[0]==t[0])return 1; Else return 0;    } getNext ();  for(i=0; i<slen;i++)    {    //printf ("***%d%d\n", i,j);         while(j>0&AMP;&AMP;S[I]!=T[J]) j=Next[j]; if(S[i]==t[j]) J + +; if(j==Tlen) {ans++; J=0; //if the substring can overlap j=next[j];        }    }    returnans;}intMain () {//use next array to find the length of the loop section      while(~SCANF ("%s", T)) {Tlen=strlen (t);       GetNext (); //for (int i=0;i<=tlen;i++) printf ("%d%d\n", I,next[i]);        if(tlen% (Tlen-next[tlen]) = =0&&next[tlen]!=0) printf ("%d\n", tlen-Next[tlen]); Elseprintf"%d\n", Tlen); }/*//KMP pattern Matching while (~scanf ("%s%s", S,t)) {Slen=strlen (s);      Tlen=strlen (t);        printf ("%d\n", Kmp_index ());      printf ("%d\n", Kmp_count ());    for (int i=0;i<=tlen;i++) printf ("%d%d\n", i,next[i]); }*/    return 0;}
View Code

Thoroughly understand baked bun slice 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.