Naïve pattern matching algorithm for graphical strings

Source: Internet
Author: User

A simple pattern matching algorithm for reviewing strings

Pattern matching:

Sub-string positioning operation, in the main string to find out where the substring appears.

In string matching, the main string S is called the target (string), and the substring T is called the pattern (string). If the substring T is found in the main string s, then the match succeeds, returning the ordinal of the first character in the main string s in the first and substring T , otherwise, the call match fails and returns 0.

Algorithm idea:

From the main string s POS character character and the first character of the pattern T comparison, if the same, then the two sequentially to compare each subsequent character, otherwise from the main string s next word character and the mode T character comparison. (Why is it simple, because of the stupidity, the factor string and the main string of each lying comparison, when found that the match is not correct, then the main string of the pointer to go back to the last character at the beginning compared to the next character at the place, to be more than once!) Strenuous).

Detailed illustrations;

Given two strings, S and T, length is known.

-》

Initial AB is the same, can be compared sequentially, when 3, does not match. Then J goes back to T1, I returns to the next character S2 of S, and compares from new start to T1.

-》

B and a Do not match, J back to 1 (position unchanged), I back to the next character, that is, 3, continue than, match, in sequence comparison. until below;

J of the pattern string goes back again to 1,i to 4, continues to compare, does not match, J of T continues to backtrack 1,s I continues to the next character, continues to compare until i=6, matches

     

Continue to compare sequentially, until T is over, that is, after j=5,i=10, J, I continue to + +, to determine the ratio is complete. This is the whole process. The arithmetic is the thought, the understanding thought, is the first step, the mind has the clear thought and the perfect scene reappearance, then the code realization All is the thing which the fulfillment.

Write in code as follows:

1 intGetLength (Char*str)2 {3     inti =0;4     5      while(' /'!=Str[i]) {6i++;7     }8     9     returni;Ten } One  A intStrcompare (Char*strmain,Char*strsub,intindex) - { -     intImain =index; the     intJsub =0; -     intLenmain =GetLength (strmain); -     intLensub =GetLength (strsub); -      +      while((Imain >=0&& Imain <= Lenmain-1) && (Jsub >=0&& jsub <= Lensub-1))){ -         if(Strmain[imain] = =Strsub[jsub]) { +imain++; Ajsub++; at}Else{ -Imain = Imain-jsub +1;//back to the next position of the main string, beginning to compare, each time the re-start comparison, the length of the IJ Walk is the same, if starting from 0, then subtract, so +1 to the next, if it is from 1, then +2 to the next bit.  -Jsub =0; -         } -     } -     //if the match is OK, the substring is the first to finish.  in     if(Jsub > Lensub-1) { -         returnImain-lensub;//The position of the first character matching the first character of the pattern string in the main string after the match OK is obtained . to}Else{ +         return 0;//Match failed -     } the } *  $ intMainintargcConst Char*argv[]) {Panax Notoginseng     Char*STR1 ="Sawtsafvda"; -     Char*STR2 ="SAFV"; the      +     inti = Strcompare (str1, STR2,0); A      theprintf"%d\n", i); +      -     return 0; $}

4

Program ended with exit code:0

Analyze the complexity of time

At worst, the final match succeeds, for example, 0000000000001 and 00001, comparing each time in 00001 of the 1 start mismatch, the pointer back to the beginning, the main string also backtracking i-j+1, if the length of the pattern substring is m, the length of the target string is N, At this time the worst case is every time the comparison in the final appearance, that is, each time the most compared to M times, up to compare n-m+1 times, the total number of comparisons is M (n-m+1), so the simple pattern matching algorithm O (m*n), although the simple pattern matching, time complexity is larger, but in practice, In general (unless there is a lot of partial matching between the pattern string and the main string, because at this time there is a lot of time to compare, the multiplication can not be approximated), the real execution times are approximate to O (n+m), so today still has his usefulness!

Naïve pattern matching algorithm for graphical strings

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.