1. Naïve algorithm: the act of Violence
The disadvantage is that the information provided by the displacement S is not fully utilized . For example P=aaab, the displacement s=0 is found to be effective. The displacement is not a valid bit because t[4]=b.
Time Complexity: O ((n-m+1) m)
2. Rabin-karp
The use of number theory: if a==b, then a≡b (mod q), if a≠b (mod q), there must be a≠b
Convert P[m],t[s,s+m] to a numeric value, and then compare the values. If not, there must be p[m]≠t[s,s+m]; if equal, then p[m] may ==t[s,s+m], then p[m] and T[s,s+m] one by one detection.
Because the ts+1 can be deduced directly from TS, the complexity of the algorithm is reduced.
The algorithm goes in two steps: 1. Preprocessing: Calculates the value of the mode P and T[m]. Time complexity of O (m)
2. Match: Time Complexity O ((n-m+1) m), because each number can be a valid displacement.
In practice, however, the possible effective displacements are very small, so the expected matching time of the algorithm is O ((n-m+1) +cm) =o (n+m)
3. String matching automata: For this abandoned therapy .....
4.KMP algorithm
In two steps: 1. Preprocessing, calculation mode p prefix function prefix[], complexity O (m)
2. Detect the matching string, according to prefix[] to calculate the effective displacement, the complexity O (n)
Where Prefix[q] is the longest length of a string of pattern PQ that is both a true prefix and a true suffix.
The algorithm code is as follows:
5.BM algorithm
String matching algorithm--summary