Model-matching BF (Brute Force) Violence algorithm

Source: Internet
Author: User
Tags comparison
Pattern matching is the substring positioning, is the process of finding the first occurrence of a substring in the main string, and finding a successful return to the location, otherwise the return-1  
match process is what?
   first the characters in the first position in the main string and the substring are compared (i,j the position of the main string, the substring respectively), so the i=1,j=1 is initialized, and if the character is equal, the characters in the next position of the two strings are compared, or I goes back to the last position of the previous comparison, J goes back to 1, Continue with the above comparison, when scanning any one of the strings is the end of the loop, if j=t[0]+1, the matching success

    light so that may not be clear, look at the picture  

Here's the code:
/* Model-matching BF (Brute Force) Violence algorithm */# include<iostream> # include<string> using namespace std;
int PATTERNMATCH_BF (string s,string t);
    int main () {string S;

    String T;
    cout << "Please enter the main string:";
    Cin >> S;
    cout << "Please enter a substring:";

    CIN >> T;
    int result = PATTERNMATCH_BF (s, t);
    if (result==-1) cout <<endl<< "Match failed" << Endl;
    else cout << endl<< The position of the substring in the main string is: << result << Endl;
return 0;
    } int PATTERNMATCH_BF (string s, String t)//return substring T at the first occurrence of string s (starting from 1), if T is not a substring of S//return-1 {
    int i = 1, j = 1;
        while (I <=s.length () && J <=t.length ())//Two strings are not scanned {if (s[i-1] = = T[j-1])//The character is equal on the position, compare the next character
            {i++;
        j + +; } else {i = i-j + 2;//Otherwise, I is the next position of the last scan position j = 1;//j from 1}} if
    (J > T.length ()) return (I-t.length ()); return-1;
} 

Although the Black Cat white cat can catch the mouse is a good cat, but, the algorithm, it must be focused on efficiency, to see the time complexity of the BF algorithm:
  in the case of successful matching, consider two extreme cases,
   1): The best case:
           each match does not succeed in the first pair of character comparisons ( For example S: "12121212345",    
           T "345"), assuming that the match successfully occurred in the first character of S, then the previous I match in a
           total comparison of i-1 times, the first time I compare m (the length of the substring T) times, so the comparison of i+m-1 times,
           All successful matches may have a total of n-m+1 (the length of the main string s),              
           p=1/(n-m+1) in the case of equal probability of each match succeeding,
           so the average number of comparisons is:
            p* (1+m-1) +p* (2+m-1) +...+p* (n-m+1 +m-1)
  The result of the simplification is (n+m)/2
      * * So the best case of time complexity is O (n+m) * * *

2) Worst case scenario:

   Each unsuccessful match occurs at the last character comparison of T (for example,
   s: "343434345", T: "345") to


   match the first character of S, then the previous i-1 comparison is compared (i.
   1) *m times, The first successful match compares M times, so a total comparison of the i*m times, so the worst case of the
   comparison of the number of flat are:
p* (1*m) +p* (2*m) +...+ (i*m) = (m* (n-m+2))/2

So, the worst case time complexity is O (n*m) (think M is much smaller than n, so ignore m*m)

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.