Brute force method string matching

Source: Internet
Author: User

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.

#include "stdafx.h"
#include <iostream>
#include <vector>
using namespace std;
/*int Main ()
{
int n;
cin>>n;
Vector<char>a (n);
for (int i=0;i<n;i++)
{
Cin>>a[i];
}
int m;
cin>>m;
Vector<char>b (m);
for (int i=0;i<m;i++)
{
Cin>>b[i];
}
Int J;
for (int i=0;i<n-m;i++)
{
j=0;
while (J<m&&b[j]==a[i+j])
{
J + +;
if (j==m)
{
cout<< i<<endl;
}
cout<< -1<<endl;
}
}
return 0;
} */
//Note: The above is from the text of the implementation of the method, because I myself on this principle although there is a certain understanding of the implementation or there is a certain flaw, so referring to the solution on the Internet
int Bruceforcestringmatch (String text,string Pattern)
{
//Gets the length of the text and the pattern respectively
int n=text.size ();
int m=pattern.size ();
//Start brute Force character match
for (int i=0;i<n-m;i++)
{
int j=0;
while (J<m&&text[i+j]==pattern[j])
{
J + +;
}
if (j==m)
{
return i;
}
}
Return-1;

}
int main ()
{
String text ("Hello world!");
String pattern ("O wo");
int Result=bruceforcestringmatch (Text,pattern);
cout<<result<<endl;
return 0;
}
Note: The two methods are implemented in the same way that the only difference is in the handling of strings.

Brute force method string matching

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.