String Simple matching algorithm

Source: Internet
Author: User

KMP string pattern matching the popular point is an efficient algorithm for locating another string in one of the strings. the time complexity of the simple matching algorithm is O (m*n), KMP matching algorithm, can prove that its time complexity is O (m+n). . Simple Matching algorithm First look at a function of a simple matching algorithm:
int INDEX_BF (char s[], char t[], int pos) {  int i = pos, j = 0;  while (s[i+j]! = ' style= ' && t[j]! = ' \")   if (t[j] = =)) &nb Sp           J + +; Continue comparison after character         Else   {        & nbsp       i + +; j = 0; Restart a new round of matches      }        if (t[j] = = ' + ')            return i; Match successfully returned subscript   else   return-1; String s in (POS character) does not exist with string t the same substring}  

The idea of this algorithm is straightforward: the main stringSa location inIthe starting substring and pattern stringTcompared. That from j=0 from comparison s[i+j] with the T[j], if equal, in the main string S exists in the i To match the probability of success for the starting position, continue to compare(Jgradually increase1), until withTThe last word in the string typeface wait, or change fromSthe next word of the string character to start the next round again"Match", the upcoming stringTSlide backward one bit, i.e. i Increase1, while J Back to0and start a new round of matches again. For example: in a strings="Abcabcabdabba"Find int= "Abcabd"(we can assume from subscript0start):First, compareS[0]and theT[0]are equal, and then compares[1] and theT[1]is equal... ..we find that we have been comparings[5] and theT[5]not wait. T s subscript backtracking length and Span style= "word-wrap:normal; Word-break:normal; line-height:20px ">t Span style= "word-wrap:normal; Word-break:normal; line-height:20px ">s subscript add 1, This time there was a mismatch,theT subscript goes back to the beginning,theS subscript increases by 1, and then it is compared again. This time there was a mismatch,theT subscript goes back to the beginning,theS subscript increases by 1, and then it is compared again.

There's been a mismatch again, soTthe subscript goes back to the beginning,SSubscript Increase1,then compare again. This timeTall the characters in the Nonalphanumeric andSmatches the corresponding characters in the. function returnsTin theSThe starting subscript in the3.

String Simple matching algorithm

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.