Detailed description of KMP algorithm and kmp Algorithm
1/* The next array is the key to the KMP algorithm. The next array is used when the mode string T and the main string S do not match 2 *, the element corresponding to the next array guides which element of the T string should be used for the next round of matching. The 3 * next array is related to the T string and is irrelevant to the S string. The key to KMP is the method of the next array. 4*5 * limit 6 * | T | 9 | a | B | a | 7 * limit 8 * | index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 9 * limit 10 * | next | X | 0 | 1 | 1 | 2 | 3 | 4 | 2 | 2 | 3 | *--------------------- --------------------------------------------- 11*12 step 0: 13 * j = 0; I = 1; next [1] = 0 14 * | 1 2 3 4 5 6 7 8 9 10 15 * Listen 16 * I = | 1 17 * j = | 0 18*19 step 1: 20 * j = 0; --> I = 2; j = 1; next [2] = 1; 21 * | 1 2 3 4 5 6 7 8 9 10 22 * ------------------------------------------------------------------- 23 * I = | 1 2 24 * J = | 0 1 25*26 step 2: 27 * T [2]! = T [1]; --> j = next [1] --> j = 0; I = 2 28 * | 1 2 3 4 5 6 7 8 9 10 29 * Limit 30 * I = | 1 2 31 * j = | 0 1 0 32*33 step 3: 34 * j = 0 --> j ++; I ++; --> j = 1; I = 3; next [3] = 1 35 * | 1 2 3 4 5 6 7 8 9 10 36*37 * I = | 1 2 2 3 38 * j = | 0 1 0 1 39*40 step 4: 41 * T [3] = T [1]; --> I ++; j ++; --> I = 4, j = 2; next [4] = 2 42 * | 1 2 3 4 5 6 7 8 9 10 43*44 44 * I = | 1 2 2 3 4 45 * j = | 0 1 0 1 2 46*47 step 5: 48 * T [4] = T [2]; --> I ++; j ++ --> I = 5, j = 3 --> next [5] = 3 49 * | 1 2 3 4 5 6 7 8 9 10 50*51 * I = | 1 2 2 3 4 5 52 * j = | 0 1 0 1 2 3 53*54 step 6: 55 * T [5] = T [3]; --> I ++, j ++; --> I = 6, j = 4; next [6] = 4; 56 * | 1 2 3 4 5 6 7 8 9 10 57*58 * I = | 1 2 2 3 4 5 6 59 * j = | 0 1 0 1 2 3 4 4 60*61 step 7: 62 * T [6]! = T [4]; --> j = next [j]; --> j = 2; 63 * | 1 2 3 4 5 6 7 8 9 10 64*65 * I = | 1 2 2 3 4 5 6 66 * j = | 0 1 0 1 2 3 4 2 67*68 step 8: 69 * T [2]! = T [6]; --> j = next [j]; --> j = 1; 70 * | 1 2 3 4 5 6 7 8 9 10 71*72 * I = | 1 2 2 3 4 5 6 6 73 * j = | 0 1 0 1 2 3 4 2 1 74*75 step 9: 76 * T [1] = T [6]; --> I ++, j ++; --> I = 7, j = 2; next [7] = 2; 77 * | 1 2 3 4 5 6 7 8 9 10 78 * --------------------------------------------------------------------- 79 * I = | 1 2 2 3 4 5 6 6 6 7 80 * j = | 0 1 0 1 2 3 4 2 1 81*82 step 10: 83 * T [2]! = [7]; --> j = next [j]; --> j = 1; 84 * | 1 2 3 4 5 6 7 8 9 10 11 85 * Listen 86 * I = | 1 2 2 3 4 5 6 6 6 7 87 * j = | 0 1 0 1 2 3 4 2 1 1 88*89 step 11: 90 * T [1] = T [7]; --> I ++, j ++; --> I = 8, j = 2; --> next [8] = 2; 91 * | 1 2 3 4 5 6 7 8 9 10 11 12 92 *--------------------------------------------------------------------------------- -- 93 * I = | 1 2 2 3 4 5 6 6 6 7 8 94 * j = | 0 1 0 1 2 3 4 2 1 2 1 1 2 95*96 step 12: 97 * T [2] = T [8]; --> I ++, j ++; --> I = 9, j = 3; next [9] = 3; 98 * | 1 2 3 4 5 6 7 8 9 10 11 12 99 * large 100 * I = | 1 2 2 3 4 6 6 6 7 8101 * j = | 0 1 0 1 2 3 4 2 1 1 2102 **/103 104 105/* mode string: ababaadacc106 * prefix and suffix concept: 107 * Prefix: remove the last element All other consecutive strings containing the first element 108 * for the pattern string d, the prefix is: a AB aba abab ababa 109 * Suffix: except the first element, all consecutive strings containing the last element are 110 * for d in the mode string, the prefix is: a aa baa abaa babaa111 **/112 113 # include <stdio. h> 114 115 // function of the next array: When the pattern string matching fails, the corresponding element of the next array indicates 116 // start from the position of the mode string and start with the element that is not matching the current main string by 117 // start with the next round of matching. 118 119 120 void getNext (char * T, int * next) {121 int I = 1; // The value of I is the subscript of the next to be calculated. 122 int j = 0; // prefix 123 next [1] = 0; 124 while (I <= 9) {125 if (0 = j | T [I] = T [j]) {126 // T [I] = T [j], then the value of 127 // The current next [I] is determined to be j128 // j = 0 or initial, or there is no matched prefix 129 // at this time, I and j automatically add 130 I ++; 131 j ++; 132 next [I] = j; 133} 134 else {135 j = next [j]; // role of next: when T [j] and T [I] do not match, the value of next [j] is 136 // indicates the position to be matched next time, a bit like recursion. 137} 138} 139} 140 int KMP (char * S, char * T, int startPosition) {141 int I = startPosition; 142 int j = 1; 143 int next [30]; 144 getNext (T, next); 145 while (I <= 17 & j <= 9) {// j> indicates that the match is successful, i> the error 146 if (j = 0 | S [I] = T [j]) {// j = 0; it can be understood that you want to restart 147 I ++; 148 j ++; 149} 150 else {151 j = next [j]; 152} 153} 154 if (j> 9) {155 return I-9; 156} 157 else158 return 0; 159} 160 161 162 int main () {163 char S [20] = "absfeafdababaaaba "; 164 char T [20] = "ababaaaba"; 165 int result; 166 result = KMP (S, T, 1); 167 printf ("the position is % d \ n ", result); 168 return 0; 169}