In fact, the exhaustion method of pattern matching can also be improved. The following is an improved method.Code.
[Improve version]
The strmatchimp () function is an improved function.
/* BenProgramTest various processing functions and operations of strings */ # Include <Stdio. h> # Include <Stdlib. h> # Include < String . H> // **************************************** * ************** 0 // Define User Data Types Typedef Long Int Lint; typedef Enum {False, true} bool; // **************************************** * ************** 1 // **************************************** * ************** 0 Int Stringmatch ( Char * String, Char * Substring,Int Startindex ); Int Stringmatchrecall ( Char * String, Char * Substring, Int Startindex ); Int Strmatchimp ( Char * String , Char * Substring, Int Startindex ); Int Main (Int Argc, Char * Argv []) { Int X; x = Stringmatch ( " Abcdefghijklmn " , " Lm " , 0 ); Printf ( " % D \ n " , X); x = Stringmatchrecall ( " Abcdefghijklmn " , " X " , 0 ); Printf ( " % D \ n " , X); x = Strmatchimp ( " Abcdefghijklmn " , " Lm " , 0 ); Printf ( " % D \ n " , X); getchar (); Return 0 ;} // **************************************** * ************** 1 // **************************************** * ************** 0 /* Function: simple pattern matchingAlgorithm: Dual-cycle function prototype: int stringmatch (char * string, char * substring, int startindex) function parameter: char * string: String char * substring: mode string int startindex: search Start index return value: If the match is successful, the first address of the pattern string starting in the string is returned; if the match fails, the-1 exception is returned: None */ Int Stringmatch ( Char * String, Char * Substring, Int Startindex ){ Int I, J; Int Strlen, substrlen; strlen = Strlen (string); substrlen = Strlen (substring ); // Check pointer validity and empty string // In fact, startindex> strlen (string)-strlen (substring) cannot be detected here) If (! String |! Substring |! String [ 0 ] |! Substring \ | Startindex> strlen-Substrlen \) Return - 1 ; For (I = startindex; I <= strlen-substrlen; I ++ ){ For (J = 0 ; J <substrlen; j ++ ){ If (String [I + J]- Substring [J]) Break ;} If (J = substrlen) // This is because the loop to be ended is exactly equal. Return ++ I; // I ++ cannot be used in this place. } Return - 1 ;} // **************************************** * ************** 1 // **************************************** * ************** 0 /* Function function: simple pattern matching algorithm: single-cycle backtracking function prototype: int stringmatchrecall (char * string, char * substring, lint startindex) function parameter: char * string: String char * substring: pattern string int startindex: Index start return value: If the match is successful, return the first address of the pattern string starting in the string; if the match fails, return-1 exception: None */ Int Stringmatchrecall ( Char * String, Char * Substring, Int Startindex ){ Int I, J; Int Strlen, substrlen; strlen = Strlen (string); substrlen = Strlen (substring ); // Check pointer validity and empty string // In fact, startindex> strlen (string)-strlen (substring) cannot be detected here) If (! String |! Substring |! String [ 0 ] |! Substring \ | Startindex> strlen- Substrlen \) Return -1 ; I = Startindex; j = 0 ; While (I < Strlen ){ If (String [I] = Substring [J]) { ++ J; ++ I ;} Else {I = I-j +1 ; // Backtracking J = 0 ;} If (J = Substrlen) Return I-j + 1 ;} Return - 1 ;} // **************************************** * ************** 1 // **************************************** * ************** 0 /* Function: String mode matching example: int strmatchimp (char * string, char * substring, int startindex) function parameter: char * string: String char * substring: pattern string int startindex: Return Value of the first index: If the match is successful, the index + 1 matching the first character is returned. Exception: None */ Int Strmatchimp ( Char * String , Char * Substring, Int Startindex ){ Int I, J; Int Strlen, substrlen; strlen = Strlen ( String ); Substrlen = Strlen (substring ); // Check pointer validity and empty string // In fact, startindex> strlen (string)-strlen (substring) cannot be detected here) If (! String |! Substring |! String [ 0 ] |! Substring \ | Startindex> strlen- Substrlen \) Return - 1 ; For (I = startindex; I <= strlen-substrlen; I ++ ){ For (J = 0 ; J <substrlen; j ++ ){ If ( String [I + J]- Substring [J]) Break ; Else { // If the first character of the mode, substring [0], has been matched, // Determines whether the last character substring [SubStrLen-1] matches // And so on. In this way, the efficiency can be improved at the best time, // O (M * n) is required in the worst case) If (String [I + substrlen- 1 -J]-substring [substrlen- 1 - J]) Break ;}} If (J = substrlen) // In this case, because the end loop is exactly the same, substring [J] = '\ 0' can also be used' Return ++ I; // I ++ cannot be used in this place. } Return - 1 ;} // **************************************** * ************** 1 // **************************************** * ************** 0 /* Function: calculates the largest matching string in the substring from the End Node to the header node. function prototype: int getmaxsubstringlen (const char * string) function parameter: const char * string: string return value: If yes, the maximum length is returned; otherwise, the-1 exception is returned: None */ Int Getmaxsubstringlen ( Const Char *String ){ Return - 1 ;} // **************************************** * ************** 1
The program running result is as follows:
This is complicated. You need to continue learning.