Experimental data structure string matching 4--

Good writing

Paste the code:

  1 #include <the iostream>
   2 #include <cstdio>
   . 3 #include <WINDOWS.H>
   . 4 #include <cwchar>
   . 5  
  . 6  the using  namespace STD;
   . 7  
  . 8  const  int max_str_len = 255 ; // user 255 (1 byte) within the definition of the maximum run length 
  . 9 typedef char sString [max_str_len]; // 0 string length storage unit
 10  
. 11  // generates a value equal to the string chars T 
12 is  void StrAssign (sString T, char * chars) {
 13 is      for ( int i = 0; chars[i] != '\0'; i ++ ){
 14         T[i + 1] = chars[i];
 15         T[0] = char(i + 1);
 16     }
 17 }
 18 
 19 void StrPrint(SString T){
 20     int len = (int)T[0];
 21     for(int i = 1; i <= len; i ++ ){
 22         printf("%c", T[i]);
 23     }
 24     cout<<endl;
 25 }
 26 
 27 int StrLength(SString S){
 28     int len = (int)S[0];
 29     return len;
 30 }
 31 
 32 bool Concat(SString T, SString S1, SString S2){
 33     int len1 = (int)S1[0];
 34     int len2 = (int)S2[0];
 35     if(len1 + len2 > max_str_len) return false;
 36     T[0] = char(len1 + len2);
 37     for(int i = 1; i <= len1; i ++ ){
 38         T[i] = S1[i];
 39     }
 40     for(int i = len1 + 1; i <= len1 + len2; i ++ ){
 41         T[i] = S2[i - len1];
 42     }
 43     return true;
 44 }
 45 
 46 //BF算法(暴力
47  // returns the substring t in position pos of the main character string the first occurrence of start. If not, return value is 0.
 48  // T non-empty,. 1 <= POS <= s.length 
49  int index (sString S, T sString, int POS) {
 50      int I = POS, J = . 1 ;
 51 is      the while (I <= StrLength (S) && J <= StrLength (T)) {
 52 is          IF (S [I] == T [J]) {
 53 is              I ++ ;
 54 is              J ++ ;
 55          }
 56 is          the else {
 57 is              I = I - J + 2 ;
 58             j = 1;
 59         }
 60     }
 61     if(j > StrLength(t))
 62         return i - StrLength(t);
 63     else return 0;
 64 }
 65 
 66 //求next数组
 67 void get_next(SString t, int next[]){
 68     int i = 1;
 69     next[1] = 0;
 70     int j = 0;
 71     while(i < StrLength(t)){
 72         if(j == 0 || t[i] == t[j]){
 73             i ++ ;
 74             j ++ ;
 75             next[i] = j;
 76         }
 77         else j = next[j];
 78     }
 79 }
 80 
 81 //kmp算法
 82 int kmp(SString s, SString t, int pos, int next[]){
 83     int i = pos;
 84     int j = 1;
 85     while(i <= StrLength(s) && j <= StrLength(t)){
 86         if(j == 0 || s[i] == t[j]){
 87             i ++ ;
 88             j ++ ;
 89         }
 90         else
 91             j = next[j];
 92     }
 93     if(j > StrLength(t))
 94         return i - StrLength(t);
 95     else
 96         return 0;
 97 }
 98 
99  int main ()
 100  {
 101      SetConsoleOutputCP ( 65001 );
 102      int I, * Next;
 103      char CHl [ 80 ], CH2 [ 80 ];
 104      sString S1, S2, S, Sub; // definition of strings, a first string length storage unit 
105      COUT << " Please enter a string: " ;
 106      CIN >> CHl;
 107      StrAssign (S1, CHl);
 108      StrPrint (S1);
 109      COUT << " Please enter the second string: " ;
 110     >> CIN CH2;
 111      StrAssign (S2, CH2);
 112      StrPrint (S2);
 113      COUT << " ------------------------- \ n- " ;
 114      COUT << " first string length: " << StrLength (S1) << endl;
 115      COUT << " second string length: " << StrLength (S2) << endl ;
 1 16      IF (! Concat (S, S1, S2)) {
 117          COUT << " string is too long " << endl;
 1 18          return  0 ;
119     }
120      the else {
 121          COUT << " main string length: " << StrLength (S) << endl;
 122          COUT << " main string as: " ;
 123          StrPrint (S);
 124      }
 125      COUT << " Enter substring: " ;
 126      CIN >> CH2;
 127      StrAssign (sub, CH2);
 128      COUT << " substring length: " << StrLength (sub) << endl;
 129      COUT << "---- BE matching algorithm and implementation ------ \ the n- " ;
 130     I = index (S, Sub, . 1 );
 131 is      IF (I)
 132          the printf ( " main strings and substrings at the% d characters at the first match \ n- " , I);
 133      the else 
134          the printf ( " main strings and substrings match is not found \ n- " );
 135      COUT << " ---- the KMP matching algorithm and implementation ---- \ n- " ;
 136      Next = new new  int [StrLength (Sub) + . 1 ];
 137      get_next (Sub, Next );
 138      the printf ( " sub-array is next string: " );
 139      for ( int= I . 1 ; I <= StrLength (Sub); I ++ ) {
 140          COUT * << (Next + I);
 141 is      }
 142      COUT << endl;
 143      I = KMP (S, Sub, . 1 , Next);
 144      IF (I)
 145          the printf ( " main strings and substrings first match of% d characters at \ n- " , I);
 146      the else 
147          the printf ( " main string and the substring matching is unsuccessful \ n- " );
 148      return  0 ;
 149 }

 

Guess you like

Origin www.cnblogs.com/moomight/p/11697708.html