Compare the advantages and disadvantages of the classical brute-force algorithm and the efficiency of the KMP algorithm.
#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAXSIZE typedef struct { Char Str[maxsize]; int length; }seqstring; int B_findex (seqstring s,int pos,seqstring t,int *count); int Kmp_index (seqstring s,int pos,seqstring t,int next[],int *count); void GetNext (seqstring t,int next[]); void Getnextval (seqstring t,int nextval[]); void PrintArray (seqstring t,int next[],int nextval[],int length); void Strassign (seqstring *s,char cstr[]);//The assignment of the string int strempty (seqstring s);//Determine if the string is an empty int strlength (seqstring s);//Find the length of the string degree operation void Strcopy (seqstring *t,seqstring S);//String copy operation int Strcompare (seqstring s,seqstring T);//string comparison operation int Strinsert (SEQS Tring *s,int pos,seqstring T);//string insert operation int Strdelete (seqstring *s,int pos,int len);//string delete operation int Strconcat (seqstring *t,s Eqstring S);//string connection operation int SubString (seqstring *sub,seqstring s,int poos,int len);//intercept substring operation int Strreplace (seqstring *s,seq String t,seqstring V);//string substitution operation int Strindex (SEQSTRing s,int pos,seqstring T);//string positioning operation void Strclear (seqstring *s);//empty string operation void Strprint (seqstring S);//String Output declaration #include "S Tring.h "/* Classic pattern matching algorithm brute-force*//* Assuming that the string is stored in sequential storage, then the Brute-force matching algorithm is as follows */int b_findex (seqstring s,int pos,seqstring t,i NT *count)/* starts looking for the substring T in the first POS position in the main string s, and returns the position of the substring in the main string if found; otherwise returns -1*/{int i,j; i = pos-1; j = 0; *count = 0; while (I < s.length && J < t.length) {if (s.str[i] = = T.str[j])/* If string S and string T correspond to the position of the character Equal, the next character continues to be compared */{i++; j + +; } else/* If the characters of the current corresponding position are not equal, start with the next character of string s, starting from the No. 0 character of T */{i = i-j+1; j = 0; } (*count) + +; } if (J >= t.length)/* If a string T is found in string s, the position of the substring T in the main string s is returned */{return i-j+1; } else {return-1;String t,int next[],int *count) {int i,j; i = pos-1; j = 0; *count = 0; while (I < s.length && J < t.length) {if ( -1 = = J | | S.str[i] = = T.str[j])/* If the j=-1 or the current character is equal, continue to compare the following characters */{i++; j + +; } else/* If the current character is not equal, move the pattern string to the right */{j = next[j];/* array Next save next function Value *} (*count )++; if (J >= t.length)/* matches successfully, returns the position of the substring T in the main string s, otherwise returns -1*/{return i-t.length+1; } else {return-1; }}/* The next function value/////* algorithm is described as follows */void GetNext (seqstring t,int next[]) {int j,k; j = 0; K =-1; Next[0] =-1; while (J < t.length) {if ( -1 = = k | | T.STR[J] = = T.str[k]) {j + +; k++; NEXT[J] = k; } else {k = next[k]; }}}/* Improved algorithm as follows */void Getnextval (seqstring t,int nextval[]) {int j,k; j = 0; K =-1; Nextval[0] =-1; while (J < t.length) {if ( -1 = = k | | T.STR[J] = = T.str[k]) {j + +; k++; if (t.str[j]! = T.str[k]) {nextval[j] = k; } else {nextval[j] = nextval[k]; }} else {k = nextval[k]; }}} void PrintArray (seqstring t,int next[],int nextval[],int length) {int J; printf ("j:\t\t"); for (j = 0;j < length;j++) {printf ("%3d", j); } printf ("\ n"); printf ("pattern string: \ t"); for (j = 0;j < length;j++) {printf ("%3c", T.str[j]); } printf ("\ n"); printf ("next[j]:\t"); for (j = 0;j < length;j++) {printf ("%3d", Next[j]); } printf ("\ n"); printf ("nextval[j]:\t"); for (j = 0;j < length;j++) {priNTF ("%3d", Nextval[j]); } printf ("\ n"); } void Strassign (Seqstring *s,char cstr[])//String assignment operation (the character CStr value in the constant assigned to string S) {int i = 0; for (i = 0;cstr[i]!= '; i++) {s->str[i] = Cstr[i]; } s->length = i; } int Strempty (seqstring S)//Determine if the string is empty {if (s.length = = 0) {return 1; } else {return 0; }} int Strlength (seqstring S)//To find the length of the string operation {return s.length; } void Strcopy (seqstring *t,seqstring s)//String copy operation (assigned each word in string S to T) {int i; for (i = 0;i < s.length; i++) {t->str[i] = S.str[i]; } t->length = s.length; } int Strcompare (seqstring s,seqstring T)//string comparison operation {int i; for (i = 0;i < S.length&&i < t.length; i++)//Compare the characters in two strings {if (S.str[i]! = T.str[i])//If there are different characters, return Two-character Difference {return (s.str[i]-t.str[i]); }} return (s.length-t.length);//If the comparison is complete, returns the difference of the length of the two string} int Strinsert (SEQSTRing *s,int pos,seqstring t)//String insert operation (Insert string T at pos position of string S) {int i; if (pos < 0 | | pos-1 > S->length) {printf ("Incorrect insertion position \ n"); return 0; if (s->length + t.length <= MAXSIZE)//substring is fully inserted into the string {//before inserting the substring T, move the POS character in S to the back of Len position for (i = S->length+t.length-1;i >= pos+t.length-1;i--) {S->str[i] = s->str[i-t.length]; }//Insert the string into S for (i = 0;i < T.length; i++) {s->str[pos+i-1] = T.str[i]; } s->length = S->length +t.length; return 1; The Else if (POS +t.length <= MAXSIZE)//substring is completely inserted in S, but the characters in s are truncated {for (i = maxsize-1;i > T.length +pos-1 ; i--) {S->str[i] = s->str[i-t.length]; } for (i = 0;i < T.length; i++) {s->str[i+pos-1] = T.str[i]; } s->length = MAXSIZE; return 0; } elseThe substring t cannot be completely inserted into S, and T will have characters discarded {for (i = 0;i < maxsize-pos;i++) {s->str[i+pos-1] = T. Str[i]; } s->length = MAXSIZE; return 0; }} int Strdelete (seqstring *s,int pos,int len)//String delete operation (delete the Len character starting at Pos in string S, and then move the following character forward) {int i,flag; if (pos < 0 | | Len < 0 | | pos+len-1 > S->length) {printf ("Incorrect delete location, parameter len not valid \ n"); Flag = 0; } else {for (i = pos+len;i <= s->length-1;i++) {S->str[i-len] = S->s Tr[i]; } s->length = s->length-len;//modifies the length of the string S flag = 1; } return flag; } int Strconcat (seqstring *t,seqstring s)//String connection operation (connect the string S to the back of the string T) {int i,flag; if (t->length +s.length <= MAXSIZE) {for (i = t->length; i < t->length +s.length; i++) {T->str[i] = s.str[i-t->length]; } t->length = T->length +s.leNgth; flag = 1; } else if (T->length < MAXSIZE) {for (i = t->length; I < maxsize;i++) { T->str[i] = s.str[i-t->length]; } t->length = MAXSIZE; Flag = 0; } return flag; } int SubString (seqstring *sub,seqstring s,int pos,int len)//Intercept substring operation (intercept string S in the sequence of characters starting from Pos, length len, and assign to sub) {int i; if (pos < 0 | | Len < 0 | | pos+len-1 > S.length) {printf ("parameter pos and len not valid \ n"); return 0; } else {for (i = 0;i < len;i++) {Sub->str[i] = s.str[i+pos-1]; } sub->length = Len; return 1; }} int Strindex (seqstring s,int pos,seqstring T)//String positioning operation (in the main string s in the first POS position to find the substring T, if there is a substring equal to the string T value in the main string s), Returns the first occurrence of a substring after the POS character of the main string {int i,j; if (Strempty (T)) {return 0; } i = pos; j = 0; while (I < s.length && J < t.length) {IF (s.str[i] = = T.str[j]) {i++; j + +; } else//if the characters of the current corresponding position are not equal, start with the next character of string s, starting from the No. 0 character of T, {i = i-j+1; j = 0; }} if (J >= t.length)//If a string T is found in string s, the position of the substring T in the main string s {return i-j+1 is returned; } else {return-1; }} int Strreplace (seqstring *s,seqstring t,seqstring V)//String substitution operation (if substring t exists in string s, then V replaces all substring T in string s) {int flag; int i = 0; if (Strempty (T)) {return 0; } while (i) {i = Strindex (*s,i,t);//Find the position of T in string S using the string's positioning if (i) {strdelete (S,i,strlength (T));//If a substring T is found, the string T in S is removed from flag = Strinsert (s,i,v);//Insert V if (!flag) { return 0; } i + = Strlength (V); }} return 1; } void Strclear (seqstring *s)//empty string operation {s->length = 0; } void Strprint (seqstring S)//string output declaration {int i; For(i = 0;i < s.length; i++) {printf ("%c", S.str[i]); } printf ("\ n"); } #include "string.h" int main (void) {seqstring s,t; int count1 = 0; int count2 = 0; int count3 = 0; int find; int next[40]; int nextval[40]; Strassign (&s, "Abaababaddecab"); Strassign (&t, "Abad"); GetNext (T,next); Getnextval (T,nextval); printf ("The next of pattern string T and the value of the improved Next: \ n"); PrintArray (T,next,nextval,strlength (T)); Find = B_findex (s,1,t,&count1); if (Find > 0) {printf ("Brute-force algorithm compares the number of times:%2d\n", count1); } find = Kmp_index (S,1,t,next,&count2); if (Find > 0) {printf ("Using Next's KMP algorithm compares the number of times:%2d\n", Count2); } find = Kmp_index (S,1,T,NEXTVAL,&COUNT3); if (Find > 0) {printf ("Using Nextval's KMP algorithm compares the number of times:%2d\n", COUNT3); } strassign (&s, "CBCCCCBCACBCCBACBCCBCBCBC"); Strassign (&t, "CBCCBCBC"); GetNext (T,next); Getnextval (T,nextval); printf ("The next of pattern string T and the value of the improved Next: \ n"); PrintArray (T,next,nextval,strlength (T)); Find = B_findex (s,1,t,&count1); if (Find > 0) {printf ("Brute-force algorithm compares the number of times:%2d\n", count1); } find = Kmp_index (S,1,t,next,&count2); if (Find > 0) {printf ("Using Next's KMP algorithm compares the number of times:%2d\n", Count2); } find = Kmp_index (S,1,T,NEXTVAL,&COUNT3); if (Find > 0) {printf ("Using Nextval's KMP algorithm compares the number of times:%2d\n", COUNT3); } return 0; }
The results of the operation are as follows:
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Pattern Matching Application