Introduction
String matching is the key to database development and word processing software. Fortunately all modern programming languages and string library functions help us in our daily work. But it's important to understand their principles.
String algorithms can be divided into several categories. String matching is one of them. When we refer to the string matching algorithm, the most basic method is the so-called brute force solution, which means that we need to check whether the characters in each text string match the matching string. In general, we have a string of text and a matching string (usually a match string shorter than a text string). All we need to do is to answer whether the matching string appears in the text string.
Overview
The principle of string brute force matching method is very simple. We must check that the first character of the matching string matches the first character of the text string, as described in the sheet.
We start by comparing the first character of a string of text and a match.
If they don't match we move to the second character of the text string. Now we compare the first character of the string with the second character of the text string. If they do not match we continue to move forward until we meet a match or until we reach the end of the text string.
Because the first character of the text string and the first character of the matched string do not match, we move forward to the second character of the text string. Now we compare the second character of the text string and match the first character of the string!
Assuming the first character matches, we move to the second character of the matched string and compare to the next character of the text string. As shown in the picture below.
If a character of the text string matches the first character of the matched string, we move forward to match the second character of the string and the next character of the text string.
If just because the first character of the matching string matches a character in the text string, that does not mean that the matching string appears in the text string, and only the first character appears in the text string, and the other is not. We must move the matching string forward to see if the complete match string is included in the text string.
Match string match
Code
/* --------------------------------* Date: 2015-02-05* sjf0115* title: Matching Brute force of Strings * Blog:------------------------------- -----*/#include <iostream>usingnamespaceStdintSubString (string text,stringPattern) {intm =text.size();intn = pattern.size();//brute force matching for(inti =0; i < m-n;++i) {intj =0; while(J < n &&text[I+j] = = Pattern[j]) {++j; }//while //Match if(j = = N) {returnI }//if}//for return-1;}intMain () {string text("Hello world!");stringPattern"O wo");intresult = SubString (text, pattern); cout<<"Subscript Location"<<result<<endl;return 0;}
Complexity of
As I said, this algorithm is slow. In fact, each algorithm, as long as it contains the word "brute force" in its name, the algorithm is very slow, its time complexity is O (n*m). Here m is the length of the text string, and n is the length of the matched string.
Original connection
Computer Algorithms:brute Force String Matching
[Ten-second algorithm series] match the brute force of the string