String pattern matching--from BF to KMP

Source: Internet
Author: User

(reprinted from a friend's blog http://blog.csdn.net/msdnwolaile/article/details/51287911#comments)

Read a lot of books and information about the KMP algorithm, the general feeling did not say very clearly, why will produce next array, why give a so short program, not a process, while some posts although next and its string matching said very clearly, but some of the reasoning process is quite complex, more abstract, Here is a simple mention of my understanding here today, as much as possible to make this process simple and easy to understand


From the beginning of the pattern match: We first studied the BF algorithm, given our frequent need to backtrack, we always do some work, in order to increase the time and space of the algorithm, we introduce the next array (as for why the time is raised, which is mentioned below), that is, the next array, we don't need to backtrack, Can be directly compared, which is also the KMP algorithm


So said: From the BF to KMP, just a simple dislike of the BF algorithm, the time spent too much space is too long, everything is progressing ...


Pattern matching or string matching of strings:

A substring is positioned to find the first occurrence of a substring from the POS character in the main string.


The main string s is usually called the target string, and the substring T is called the pattern string.



BF algorithm:


Brute-force, also known as Brute Force (violent) matching algorithm

1, starting with the POS character of the main string s, and comparing it with the first character of the pattern string T.

2, if equal, then each successive character is compared; otherwise, it goes back to the first pos+1 character of the main string, continue to compare with the first character of the pattern string T, repeat step 2, knowing that each character in the pattern string T is equal to the main string (returning the first character on the current main string s match) or characters the main string s ( All characters after POS position (return 0)),



Program 1:

[CPP]  View plain  copy   #include  <stdio.h>                                                                                                                                        #include  <string.h>    #include  <malloc.h>      int bfmode (char *s, char *t,  int pos)   {           int flag1,flag;           int i,j;            int slength, tlength;           slength  = strlen (s);           tlength = strlen ( T);              for (i = pos; i  < slength; i++)            {                    flag1 = i, flag = 0;                    for (j = 0; j < tlength; j++)                     {                             if (s[i] == t[j] && i < slength)                              {                                     flag ++;                                     i++;                                }                             else                                     break;                      }                    if (flag == tlength)                     {                            return  Flag + 1;                   }                    i  = flag1;           }               return 0;  }       int  Main ()    {           char s[50];    //Target string            char t[20];   // Pattern string is also substring            int pos;      &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SCANF ("%s",  s);           &NBSP;SCANF ("%s",  t);       &n

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.