Sunday algorithm template, Sunday Algorithm
Sunday is a linear string pattern matching algorithm. The concept of the algorithm is as follows: Sunday is a string pattern matching algorithm proposed by Daniel M. Sunday in 1990. The core idea is: During the matching process, the pattern string is not required to be compared from left to right or from right to left. When a mismatch is found, the algorithm can skip as many characters as possible to perform the next matching, thus improving the matching efficiency. The recording mode string is S, the substring is T, and the length is N, M, respectively. For T, we make a simple and clever preprocessing: Record TPosition of the last occurrence of each characterTo save it to an array. Assume that S [I] ≈ T [j], 1 ≤ I ≤ N, 1 ≤ j ≤ M in case of mismatch. Set the first matching character position of S to L. Obviously, S [L + M + 1] must participate in the next round of matching, and T must at least match S [L + M + 1] to match the whole S. Now we are looking for the location where S [L + M + 1] appears in T. Using the pre-processed array, We can O (1) Find the location u and move it directly to T [u] = S [L + M + 1]. In particular, if S [L + M + 1] does not appear in T, T cannot match S [L + M + 1, then, the first part of T is directly moved to S [L + M + 2] to continue matching. The matching is completed until L + M> N. The idea of the Sunday algorithm is similar to that of the BM algorithm. When a match fails, the next character of the last character to be matched in a text string is concerned. If this character does not appear in the matching string, skip it directly, that is, move the step size = length of the matching string + 1; otherwise, the same as the BM algorithm, the moving step = the distance from the rightmost character to the end of the matching string + 1. Algorithm example S: abcceabcaabcdT: abcd found that d and c do not match. In this case, S [L + M + 1] = 'E' does not appear in T. So: S: abcceabcaabcdT: -------- abcd found that d and a do not match. In this case, S [L + M + 1] = 'A', T is the most recent T [0]. So: S: abcceabcaabcdT: -------------- abcd is matched successfully.
1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 using namespace std; 5 int wei [301] = {0}; 6 int ans = 0, lend, lenc, tot = 0; // tot is used to count the number of matching times, which is convenient for comparison with other algorithms 7 char c [10001], d [10001]; 8 void pei () 9 {10 int w = 0; // number of rows that are moved to the right after the matching failure of record d 11 while (w + lend <= lenc) 12 {13 int I = 0; // The number of BITs being matched. 14 bool f = false; // The number of MnS is successfully matched. 15 while (I <= lend & f = false) 16 {17 if (c [I + w]! = D [I]) 18 f = true; // match failed 19 I ++; tot ++; // match the next digit, matching times + 1 20} 21 if (f = false) 22 {ans ++; 23 cout <I <endl; 24 w ++ ;} // If the matching succeeds, the entire string B is shifted to the right by 25 else matching with the next digit of string a. // The matching fails. 26 {27 I = lend + 1; // directly match the position where string B appears again in string a 28 if (wei [c [I + w] =-1) 29 w = w + I + 1; // If no result is returned, the entire string B is shifted to lend + 1-bit 30 else w = w + I-wei [c [w + I]; // if it appears, jump to the position where it appears? 31} 32} 33 return; 34} 35 int main () 36 {37 gets (c); 38 gets (d); 39 lenc = strlen (c)-1; 40 lend = strlen (d)-1; 41 for (int I = 0; I <= 300; ++ I) wei [I] =-1; 42 for (int I = 0; I <= lend; ++ I) 43 wei [d [I] = I; // record the location of each character 44 pei (); 45 if (ans) 46 cout <ans <endl <tot; 47 else cout <"mission failed "; 48 return 0; 49}