Algorithm-Rabin-Karp Algorithm for String Matching

Source: Internet
Author: User

Algorithm-Rabin-Karp Algorithm for String Matching
Preface

The Rabin-Karp string matching algorithm is similar to the simple string matching algorithm described earlier. It also compares each character. The difference is that Rabin-Karp uses character preprocessing, that is to say, the corresponding hexadecimal number and modulo operation are performed for each character. It is similar to calculating its function value through a function and comparing the function value of each character. The pre-processing time is O (m), and the matching time is O (n-m + 1) m ).

Implementation of Rabin-Karp Algorithm

Pseudocode:

 

Rabin_Karp_search (T, P, d, q) n = T. length; m = P. length; h = d ^ (m-1) mod q; p = 0; t = 0; for I = 1 to m p = (d * p + P [I]) mod q; t = (d * t + T [I]) mod q; for I = 0 to n-m if p = t if P [1 .. m] = T [I + 1 .. I + m] printPattern occurs with shifti if I
 
  

 

Source code:

 

// Rabin Karp Algorithm  #include
   
    #include
    
     using namespace std; void Rabin_Karp_search(const string &T, const string &P, int d, int q){    int m = P.length();    int n = T.length();    int i, j;    int p = 0;  // hash value for pattern    int t = 0; // hash value for txt    int h = 1;      // The value of h would be pow(d, M-1)%q    for (i = 0; i < m-1; i++)        h = (h*d)%q;      // Calculate the hash value of pattern and first window of text    for (i = 0; i < m; i++)    {        p = (d*p + P[i])%q;        t = (d*t + T[i])%q;    }      // Slide the pattern over text one by one     for (i = 0; i <= n - m; i++)    {                // Chaeck the hash values of current window of text and pattern        // If the hash values match then only check for characters on by one        if ( p == t )        {            /* Check for characters one by one */            for (j = 0; j < m; j++)               if (T[i+j] != P[j])   break;                        if (j == m)  // if p == t and pat[0...M-1] = txt[i, i+1, ...i+M-1]               cout<
     
      

 

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.