Objective: To determine the first occurrence position of a substring in the primary string: BF, KMP (Key Points) 1: BF algorithm 1. features: the pointer of the Main string needs to be traced back, and the speed is slow; 2. algorithm idea: When the comparison characters of the primary string T (m) and the substring S (n) are not equal, the pointer to the primary string I needs to point to the next character (corresponding to
Objective: To determine the first occurrence position of a substring in the primary string: BF, KMP (Key Points) 1: BF algorithm 1. features: the pointer of the Main string needs to be traced back, and the speed is slow; 2. algorithm idea: When the comparison characters of the primary string T (m) and the substring S (n) are not equal, the pointer to the primary string I needs to point to the next character (corresponding to
Algorithm Purpose: Determines the position where the substring first appears in the main string
Two algorithms:BF and KMP (key points)
I. BF algorithm
1. features: the pointer of the Main string needs to be traced back, and the speed is slow;
2. algorithm ideas:
When the comparison characters of the primary string T (m) and the substring S (n) are not equal, the pointer I of the primary string needs to point to the next character at the position where the comparison was started. (The pointer j of the corresponding substring needs to be pointed to 1 again ),, in this way, we can compare the substring T with a consecutive substring of the primary string to know that the two strings are equal.
Int Index_BF (SString S, SString T, int pos) // the position from which the pos is located, set the subscript of both strings to start from 1 {if (pos <= 0 | T. length <= 0) return 0; // The int I = pos, j = 1; while (I <= S. length & j <= T. length) {if (S. ch [I] = T. ch [j]) {I ++; j ++} else {I = I-j + 2; j = 1 ;}} if (j> T. length) return i-T.length; // or I-j + 1 else return 0; // not found}3. Time Complexity Analysis:
Best case: Compare only once, that is, compare the number of times of the substring length n = O (n );
Worst case: the last character of the substring is not the same as the primary string during each comparison. Therefore, we need to compare (m-n) * n + n = (m-n + 1) * n = O (m * n) times
General Condition: O (m + n); // calculate the total number of comparisons from the best to the worst, and then take the average value.
Ii. KMP algorithm (I am still not very familiar with the detailed reasoning process, but I will be able to master the following ):
1. Features: During comparison, the pointer I of the Main string does not need to be traced back. You only need to slide the sub-string several distances to the right.
2. Idea: Make full use of partially matched result information, and try to avoid backtracking I to speed up the Sliding Speed of the pattern string.
3. Evaluate k = next [j]:
1 ). j indicates the mismatch between the child string being compared and the master string, k = next [j] indicates the position of the character pointer of the substring when the next primary string is compared with the substring;
2). The next [j] function indicates the maximum length of the substrings with the same prefix and suffix (true substrings) in mode T.
It can be seen that the more similar parts in the mode, the greater the next [j] function, the higher the degree of relevance between T characters in the mode, it also indicates that the larger the number of characters that match the main string before the j position.
That is, the larger next [j], the longer the mode string slides to the right. The less the number of comparisons with the master string, the lower the time complexity (time efficiency ).
3). Method: (for derivation, see < <数据结构> > Original)
4. Code Implementation (evaluate the next [j] function ):
Void get_next (SString T, int & next []) {// evaluate the next function value of mode string T and store it in the array next []. I = 1; next [1] = 0; j = 0; while (I
5. Complete KMP implementation:
Int Index_KMP (SString S, SString T, int pos) // compare with BF algorithm (similar) {if (pos <= 0 | T. length <= 0) return 0; // invalid operation I = pos; j = 1; while (I <= S. length & j <= T. length) {if (j = 0 | S. ch [I] = T. ch [j]) {++ I, ++ j} // if no mismatch exists, compare the subsequent characters else {j = next [j];} // features: the I pointer of S does not backtrack, and matches if (j> T. length) return i-T.length; // The End Of The substring, indicating that the matching is successful else return 0; // not found} // Index_KMP
6. Discuss whether the current next [j] function is complete:
1) assume that the primary string T is a B,
Substring S is a B;
Obtain next [j] =, 2, 3, 4 of S;
We can simulate the complete KMP algorithm above and find that when the pointer I of the master string and the pointer j of the Child string both point to 4, this will not match, j = next [4] = 3, and mismatch found. j = next [3] = 2, and mismatch ..... in turn, j points to 0, And then I = 5, j = 1 to match. In this process, we can find that the KMP algorithm does not play a role, because the substrings have many identical prefixes, as a result, the primary string does not match the child string multiple times, that is, the next [j] function is not perfect !!!!
2). Complete next [j] algorithm:
Void get_nextval (SString T, int & nextval []) {// Save the corrected value of the next function to the array nextval I = 1; nextval [1] = 0; j = 0; while (I
7. Time Complexity Analysis:
Since pointer I does not need to be traced back, the number of comparisons is only m, even if n is used to calculate next [j, the total number of comparisons is only m + n = O (m + n), which is much faster than the BF algorithm.