#include <stdio.h> #include <stdlib.h> #include <conio.h> #include <string.h>int violentmatch (char* S, char* p) {int slen = strlen (s); int plen = strlen (p); int i = 0 ; int j = 0 ; while (I < Slen && J < Plen) {if (S[i] = = P[j]) {//① If the current character matches successfully (i.e. s[i] = = P[j]), then i++,j++ i++ ; j + + ;} else {//② if mismatch (i.e. s[i]! = P[j]), so i = i -(j-1), j = 0 i = i-j + 1 ; j = 0 ;}}//Match succeeded, return the position of the pattern string p in the text string s, otherwise return-1 if (j = = Plen) return I- J else return-1 ;} void main () {char txt[] = {"Asdasdfasffds" }; char pat[] = {"fasf" }; int i = Violentmatch (TXT, PAT); if (i! =-1 ) {printf ("string matches at%d locations \ n", i+1 );} else printf ("Match failed!") ); Getch ();}
Run Result:
String match at 7th position
For example, if a given text string S "BBC Abcdab Abcdabcdabde", and the pattern string P "abcdabd", now to take the pattern string p to match the text string s, the whole process is as follows:
1. s[0] for b,p[0] is a, does not match, executes the ② directive: "If mismatch (that is, s[i]! = P[j]), so i = i-(j-1), j = 0 ", s[1] match p[0], equivalent to the pattern string to move to the right one (i=1,j=0)
2. s[1] and p[0] still do not match, continue to execute the ② directive: "If mismatch (that is, s[i]! = P[j]), so i = i-(j-1), j = 0 ", s[2] match p[0] (i=2,j=0), so that the pattern string continues to move to the right one (constant execution" i = i-(j-1), j = 0 ", I from 2 to 4,j has been 0)
3. Until s[4] matches the p[0] (i=4,j=0), at this point in accordance with the above-mentioned brute force matching algorithm, instead of the ① instruction: "If the current character matching success (ie s[i] = = P[j]), then i++,j++", can get s[i] for s[5], P[J] (p[1], i.e. next s[5] match p[1] (i=5,j=1)
4. s[5] Match p[1] successfully, continue to execute the ① directive: "If the current character matches successfully (ie s[i] = = P[j]), then i++,j++", Get S[6 "and P[2] Match (i=6,j=2), so go on
5. Until S[10] is a space character, P[6] is the character D (i=10,j=6), because it does not match, re-executes the ② directive: "If mismatch (that is, s[i]! = P[j]), so i = i-(j-1), j = 0 ", equivalent to s[5] match p[0] (i=5,j=0)
6. So far, we can see, if according to the idea of the brute force matching algorithm, although the previous text string and pattern string has been matched to s[9], p[5], but because s[10] and p[6] does not match, so the text string back to S[5], pattern string back to p[0], so that s[ 5] match with P[0].
and S[5] definitely mismatch with p[0. Why is it? Because in the previous 4th match, we have learned that s[5] = p[1] = B, and p[0] = A, that is, p[1]! = p[0], so s[5] must not be equal to p[0], so backtracking in the past will inevitably lead to mismatch. Is there an algorithm that allows I not to go back, just move J?
The answer is yes. This algorithm is the main thrust of this paper KMP algorithm, it uses the previously already partially matched this effective information, keep I do not backtrack, by modifying the position of J, let the pattern string as far as possible to move to a valid position.