The Bruker-FOSS algorithm
The simple pattern matching algorithm is a kind of matching algorithm with backtracking.
I. Algorithmic thinking
Starting from the POS character of the main string s, and comparing with the first character of the pattern string T, if the equality continues to compare subsequent characters, and if not, then the pos+1 characters from (back to) the main string s begin to be compared with the pattern string T. Until each character in the pattern string T and each successive character sequence of the main string are all equal, the match succeeds, returns the position of the character equal to the first character of T in the main string s, or does not have a sequence of characters equal to the pattern string in the main string.
Two. Algorithm implementation code
Set I,j,start three indicators:
i--points to the character currently being compared in the main string s, starting with the first character of S, and thereafter each comparison step, backward one position, a trip to match failed, back to the next position of the comparison starting point.
j--points to the character currently being compared in the substring T, starting at the first character of T, followed by each step, then a position, and a match fails, backtracking to the first character of T.
start--records the starting position of each comparison in the main string s, after each comparison, moves back one position to determine the starting position of the next trip.
#include <stdio.h> #include"SstringHeader.h"/ * Main string s, mode string t*/intNavie_string_matcher (sstring S,intPos,sstring t) {intI,j,start; start = pos; i = start; j =0;/ * The main string starts from Pos and the pattern string starts from the beginning * / if(T.Len==0)return0;/ * When the pattern string is an empty string, it is an arbitrary string of matching strings * /while (I < S.Len&& J < T.Len)if(S.ch[i]==t.ch[j])/ * Current corresponding word typeface, etc. *{i++; j + +; }Else{start++;/ * Corresponding characters are not equal to the backtracking * /i = start;/* The main string starts from Start+1 and the pattern string starts from the beginning (0) */J=0; }if(J >= T.Len)return(start);/ * Return match start location when match succeeds * / Else return-1;/ * When the match is unsuccessful, return -1*/}
Algorithm implementation results:
Three. Algorithm implementation examples
Main string s:a b a b c a b c a c B a B
Sub-string t:a b c a C
pos = 0; Start = Pos=0;i=start = 0;j=0;
S.len = 13; T.len=5 the length of the string s,t, respectively
while (i < s.len && j < t.len ) if (s.ch[i]==t.ch[j])/*当前对应字符相等时推进*/ { i++; j++; } else { start++; /*对应字符不等时回溯*/ i = start;/*主串从start+1开始,模式串从头(0)开始*/ =0; }
(1) I=0;j=0;i<13&&j<5 is true
if (S.ch[i]==t.ch[j]), that is to Judge S.ch[0]==t.ch[0],s.ch[0]=a,t.ch[0]=a, is true,
i++;j++;
I=1;j=1;
I=1;j=1; also satisfies the character match;
i=2;j=2, S.ch[2]=a,t.ch[2]=c
(2) start=1,i=1,j=0; The process is the same as above, only give the matching result diagram:
(3) Start=2,i=2;j=0;
(4) Start=3;i=3;j=0;
(5) Start=4;i=4;j=0;
(6) Start=5;i=5;j=0;
J=5,while cycle is not satisfied;
Perform
if (j >= t.lenreturn/*匹配成功时返回匹配起始位置*/
return to Start=5;
Four. Algorithm complexity analysis
The worst time complexity of the algorithm is O ((s.len-t.len+1) *t.len)
Reference:
"Data structure-in C language description", Shang. 4.2-String Storage implementations
String matching--Naïve algorithm brute-force (the Bruker-FOSS algorithm)