String matching algorithm--summary

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.