Data structure 19:BF algorithm (normal pattern matching algorithm)

Source: Internet
Author: User

Determine whether there is a relationship between the main string and the substring between the two strings, which is called the pattern matching of a string.

In the string pattern matching process, the substring T is often called the "pattern string". Normal pattern matching ("BF" algorithm)

To determine whether there are two strings of the relationship between the string and the main string, the most direct algorithm is to take the pattern string, and the main string from start to finish, which is the "BF" algorithm implementation.

The provided pattern string (for example, "ABCAC") starts with the first character of the main string, sequentially determines whether the characters in the same position are equal, and if all equals, the match succeeds; conversely, the substring moves backwards by one character and continues to match the corresponding character in the main string.

Algorithm run: (in the figure, I and J indicate the position of the matching character in the array subscript)


, the first match, the pattern string and the main string match to the third character, the match fails, the pattern string moves to the right one character, or from the first character ' a ' and the second character of the main string ' B ' matches, the match fails; the pattern string continues to move the position of one character, continuing the match.

Implementation code:

#include <stdio.h>#include<string.h>
intSelChar*s,Char*T)
{inti =0, j =0; while(I<strlen (S) && j<strlen (T))
{if(S[i] = =T[j])
{i++; J++; }
    Else
    {i= i-j+1; J=0; }  }
  //There are two possibilities for jumping out of the loop, I=strlen (S) indicating that the main string has been traversed, J=strlen (T), indicating that the pattern string traversal is complete and that the successful match in the main string    if(J = =strlen (T))
{returnI-strlen (T) +1; }//run to this, for the case of I==strlen (S)    return 0;}
intMain ()
{intAdd = SEL ("Ababcabcacbab","ABCAC"); printf ("%d", add);
  return 0;}

Operation Result:6

Time complexity of the "BF" algorithm

The "BF" algorithm in the most ideal case of time complexity is O(m) (M is the length of the pattern string, that is, the first match on the success of the case).

In general, the time complexity of the "BF" algorithm is O(n+m) (n is the length of the main string, and M is the length of the pattern string).

The worst case of the time complexity is (for example, the O(n*m) main string S is "000000000001", the pattern string T "001", each match, until the last element to match, only to know that the match failed, run N*m times).

Summarize

"BF" algorithm in the pattern matching, starting from the first character of the main string, each failure, the pattern string to move backward one character position, continue to match, no brain operation. But the whole algorithm is affected by the test data very much, in solving the actual problem, due to the large amount of data, time complexity is often very high.

Therefore, on the basis of the "BF" algorithm, it has been improved, that is, the next SectionTo-speaking "KMP" algorithm.

Data structure 19:BF algorithm (normal pattern matching 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.