[12 of algorithm series] brute force matching of string matching and string matching of algorithm series

Source: Internet
Author: User

[12 of algorithm series] brute force matching of string matching and string matching of algorithm series

Introduction

String Matching is the key to database development and word processing software. Fortunately, all modern programming languages and string library functions help us with our daily work. However, it is important to understand their principles.

String algorithms can be divided into several types. String Matching is one of them. When we mention the string matching algorithm, the most basic method is the so-called brute force solution, which means we need to check whether the characters in each text string match the matching string. Generally, we have a text string and a matching string (usually the matching string is shorter than the text string ). What we need to do is to answer whether the matching string is in a text string.

Overview

The principle of string brute force matching is very simple. We must check whether the first character of the matching string matches the first character of the text string, as described in the section.


We start by comparing the first character of a text string and a matching string.

If they do not match the second character we move to the text string. Now we can compare the first character of the matching string with the second character of the text string. If they do not match, we move forward until we encounter a matched or until we reach the end of the text string.


Because the first character of the text string does not match the first character of the matching string, we move forward to the second character of the text string. Now we can compare the second character of the text string and the first character of the matching string!

Assuming that the first character matches, we move to the second character of the matching string to compare it with the next character of the text string. As shown in the following figure.


If one character of a text string matches the first character of the matching string, we move forward to match the second character of the matching string and the next character of the text string.

If the first character of the matching string matches a character of the text string, it does not mean that the matching string appears in the text string, only the first character appears in the text string. We must move the match string forward to see if the complete match string is contained in the text string.


Matching string matching

Code

/* -------------------------------- * Date: 2015-02-05 * Author: SJF0115 * Subject: string Matching brute force matching * blog: Example */# include <iostream> using namespace std; int SubString (string text, string pattern) {int m = text. size (); int n = pattern. size (); // brute force matching for (int I = 0; I <m-n; ++ I) {int j = 0; while (j <n & text [I + j] = pattern [j]) {++ j;} // while // match if (j = n) {return I;} // if} // for return -1;} int main () {string text ("hello world! "); String pattern (" o wo "); int result = SubString (text, pattern); cout <" subscript position-> "<result <endl; return 0 ;}

Complexity

As I said, this algorithm is slow. In fact, every algorithm, as long as its name contains the word "brute force", is very slow, and its time complexity is O (n * m ). Here m is the length of the text string, while n is the length of the matching string.

Original Text connection

Computer Algorithms: Brute Force String Matching

Related Article

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.