String Matching Algorithm

Source: Internet
Author: User

1. The regular string matching algorithm can match characters one by one from the beginning. In case of inequality, it can be traced back to continue matching one by one starting from the next position. The following is a program:

# Include
 
  
Using namespace std; # include
  
   
Int main () {int find (string, string); string s1 ("hello world"); string s2 ("world"); cout <
   <
    

The matching process is as follows:

Assume that the length of the long string is n and the length of the matched string is m. The average complexity of the above algorithm is O (m * n). If there is a string, s1 = "aaaa... B ", there are first 100 a, s2 =" aaaaab ". When the above method is used for matching, it will be traced back 95 times, which is very inefficient, to improve string matching efficiency, you can use the KMP algorithm. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + IDwvcD4KPHA + kernel/rysfA + 9PD0tG + rbHIvc + z9rXEveG5 + kernel/Co7o8L3A + kernel/a1vbK7z + C1yLXE19a3 + kernel/kernel Release/release/gtcijrM/release + IDwvcD4KPHA + release + zb/release + vK19fWt/release/vT67 + qzbe1xstmbj219a3 + 7K7tcijqLvy 1d/release/CserOqmq1xNfWt/release + 9Prv6rNt7XEa7j219a3 + 8/release + release/w8L3A + release/K/release + CjxwPiA8L3A + release "brush: j Ava; "> void next (const char * s, int next []) {int j = 0, k =-1; next [0] =-1; while (s [j]! = '\ 0') {if (k =-1 | s [j] = s [k]) {+ j; ++ k; if (s [j]! = S [k]) next [j] = k; elsenext [j] = next [k];} elsek = next [k];} for (int I = 0; I <<<< pre><<<<>



<

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.