String matching algorithm

Source: Internet
Author: User
Tags string back

Partial content Reference http://blog.csdn.net/v_july_v/article/details/7041827

1. Brute Force matching algorithm

Suppose we now face the problem of having a text string s, and a pattern string p, now to find the position of P in S, how to find it?

If the idea of a violent match, and assuming that the text string s matches now to the I position, the pattern string P matches to the J position, there are:

  • If the current character matches successfully (that is, s[i] = = P[j]), then i++,j++ continues to match the next character;
  • If the mismatch (that is, a s[i]! = P[j]), so i = i-(j-1), j = 0. Equivalent to every match failure, I backtracking, J is set to
  • #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.

String 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.