String Matching: BF and KMP Algorithms

Source: Internet
Author: User

String Matching: BF and KMP Algorithms

String Matching Algorithm: BF and KMP algorithm code.

 

/*************************************** ** Copyright (c) 2015 Jingshuang Hu @ filename: demo. c @ datetime: 2015.10.11 @ author: HJS @ e-mail: [email protected] @ blog: *************************************** **/# include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
Using namespace std; /*************************************** * **********/int text_read (char * S ); void BF_String_Matching (char * S, char * T); void KMP_String_Matching (char * S, char * T); int * Partial_Matching_Table (char * T ); /*************************************** * **********/int main () {char * S = (char *) malloc (1000 * sizeof (char); char T [100]; text_read (S); printf (given text string: % s, s); printf (input mode string :); gets (T); printf (------- BF algorithm -------); BF_String_Matching (S, T); printf (------- KMP algorithm ------); KMP_String_Matching (S, T); printf (); return 0 ;} /*************************************** * **********/int text_read (char * S) {int len = 0; char ch; FILE * fp = fopen(test.txt, r); if (! Fp) {printf (failed to open the file !); Return 1;} while (ch = getc (fp ))! = EOF) {S [len] = ch; len ++;} fclose (fp); S [len] = ''; S = (char *) realloc (S, (len + 1) * sizeof (char); return 0 ;} /*************************************** ********** // void BF_String_Matching (char * S, char * T) {int S_length = strlen (S); int T_length = strlen (T); int I = 0, j = 0, same_length = 0; for (I = 0; I <S_length-T_length; I ++) {for (j = 0; j <T_length; j ++) {if (S [I + j] = T [j]) {same_length ++ ;} Else {break ;}}if (same_length = T_length) {printf (matching found at LOCATION % d !, I + 1) ;}same_length = 0 ;}} /*************************************** ********** // KMP mobile digits = number of matched characters-the corresponding part of the matched value void KMP_String_Matching (char * S, char * T) {int * table = Partial_Matching_Table (T); // obtain the partially matched table int S_length = strlen (S); int T_length = strlen (T ); int I = 0, j = 0; int same_length = 0, move_pos = 0; for (I = 0; I <S_length-T_length; I ++) {if (S [I + 0]! = T [0]) // if the first character is not the same, move one {continue;} else // if not, continue to compare the subsequent characters {same_length ++; for (j = 1; j <T_length; j ++) {if (S [I + j] = T [j]) {same_length ++; // Number of matches} else {move_pos = table [j-1]; break ;}}if (same_length = T_length) {printf (matching found at LOCATION % d !, I + 1); I + = T_length-1; // move the T_length bit backwards} else {I + = same_length-move_pos-1; // position of I after moving} same_length = 0 ;}} /*************************************** * ********* // part of the matching table int * Partial_Matching_Table (char * T) {int T_length = strlen (T); int * table = (int *) malloc (T_length * sizeof (int); int k =-1, I = 0, j = 0; memset (table, 0, T_length); // All are initialized to 0 table [0] =-1; while (j <T_length) {if (-1 = k) | (T [k] = T [j]) {j ++; k ++; table [j] = k;} else {k = table [k] ;}} for (I = 0; I <T_length; I ++) {table [I] = table [I + 1];} table = (int *) realloc (table, T_length * sizeof (int); return table ;}
    
   
  
 

 

 

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.