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