Algorithm--order finding of brute force method and brute force string matching

Source: Internet
Author: User

Then yesterday's selection sort and bubble sort, today to implement the order lookup and brute force string matching two algorithms.

A sequential lookup is a comparison of each element of a given lookup key and List Zhou until a successful element is found to return the element subscript, the lookup succeeds, or the lookup fails to find the entire list of two without matching elements. Here is a record of the limit version of the order lookup method. The limit device is to add the lookup key to the list at the end, so that in the process of finding, you do not have to judge each time to reach the end of the list to determine whether the lookup failed, but after the search is over, determine whether the match element subscript is less than n (assuming the list has n elements) to determine whether the lookup is successful. The following is the order-finding algorithm for the limiter version:

Limit Order Lookup algorithm:

Sequentialsearch (A[0....N], K)

Input: An array of n elements a and a lookup key K

Output: The position of the element with the first value equal to K, if no such element is found, return-1

A[n]<-k

While A[i]≠k do

i++

If I<n return i;

else return-1; This algorithm is in the algorithm design and analysis, I think there is no need for the else here, when the search succeeded, has returned the main function in advance,

No more execution of return-1, in the process of code implementation, try to get rid of else, the result is also correct

The time complexity of the algorithm above is: Θ (n)

The following is the C + + implementation code for the algorithm:

#include <iostream>using namespaceStd;typedefCharElemtype;intSequentialsearch (Elemtype esearch[],intN, Elemtype K);voidMain () {elemtype a[6] = {'a','R','J','L','h'};//the defined array length is 1 larger than the original table, and the position of K is reserved .Elemtype K; CIN>>K;    GetChar (); intIlocation = Sequentialsearch (A,5, K);//here is the maximum value of the list subscript    if(Ilocation = =-1) {cout<<"lookup failed with no in list"<<K<<"Elements"; }    Elsecout <<"find success, the element location is:"<<ilocation; GetChar ();}intSequentialsearch (Elemtype esearch[],intN,elemtype K) {Esearch[n]=K; inti =0;  while(Esearch[i]! =K) {i++; }    if(I <N) {        returni; }     return-1;}

Here's a look at brute force string matching.

The algorithm's approach is to align the pattern (that is, the shorter substring, length m) with the first m characters of the text (that is, the longer string, the length n), match each pair of characters from left to right, and if the m pairs of characters are successful, the algorithm stops, or when a pair of mismatches is encountered, the pattern is shifted It then continues to match the text from the first character of the pattern. In this process, the algorithm can be stopped when the length of the remaining characters in the text is less than the pattern, since it is impossible to match the succeeding, so the subscript of the text terminates when the N-m is reached. Here is the algorithm for brute force string matching:

Brute force matching string algorithm:

Bruteforcestringmatch (T[0....n-1],p[0...m-1])

Input: An n-character array t[0...n-1] represents a piece of text, a M-character array p[0...m-1] represents a pattern

Output: Returns the position of the first character of the first matched substring in the text when the match succeeds, returns 1 on failure

For i<-0 to N-m do

J <-0

While t[i+j] = P[j] and J < m do

J + +

If J=m return I

Return-1

The time complexity of the algorithm in the worst case, that is, each pattern match to the last pair of characters when the match failed, that is, to make M-comparison, so that the n-m+1 times such a match, the algorithm is θ (nm), the book described in the search for random text, the display is linear efficiency θ (n+m) =θ (n). The following is the C + + implementation code for the algorithm:

#include <iostream>using namespaceStd;typedefCharElemtype;intBruteforcestringmatch (Elemtype t[],intN, Elemtype p[],intm);intMain () {elemtype t[Ten] = {'a','P','P','L','e','s' }; Elemtype p[3] = {'P','P','L' }; Elemtype ps[2] = {'P','s' }; intRESULT1 = Bruteforcestringmatch (T,Ten-1,3); if(RESULT1 = =-1) {cout<<"mismatch of T and P failed"; }    Elsecout <<"the T and P match successfully, and the start position of the character P in T is:"<<RESULT1;    GetChar (); return 1;}intBruteforcestringmatch (Elemtype t[],intN, Elemtype p[],intm) {    inti =0, j=0;  for(i =0; i < n-m; i++) {J=0;  while(J < m && T[i + j] = =P[j]) {J++; }        if(J = =m) {            returni; }    }    return-1;}

Algorithm--order finding of brute force method and brute force 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.