C++ Program to Implement Rabin-Karp Method for Pattern Searching
Posted by Superadmin on August 10 2022 04:15:19

C++ Program to Implement Rabin-Karp Method for Pattern Searching

 

 

This C++ program implements the Rabin-Karp method for string matching. A text and a pattern is given as input. The pattern is searched for in the text and all instances of the pattern are given as output.

 

This C++ program is successfully compiled and tested on our system. The program output is given below.

  1. /*
  2.  * C++ program implements the Rabin-Karp method for string matching.
  3.  */
  4. #include<stdio.h>
  5. #include<string.h>
  6. #include<iostream>
  7. #include<vector>
  8. using namespace std;
  9.  
  10. // d is the number of characters in input alphabet
  11. #define d 256
  12.  
  13. /*  pat  -> pattern
  14.     txt  -> text
  15.     q    -> A prime number
  16. */
  17.  
  18. void get_input(vector<char>& a)
  19. {
  20.     char c;
  21.     while (1)
  22.     {
  23.         c = getchar();
  24.         if (c == '\n')
  25.         break;
  26.         a.push_back(c);
  27.     }
  28.     return;
  29. }
  30.  
  31.  
  32. void search(char *pat, char *txt, int q)
  33. {
  34.     int M = strlen(pat);
  35.     int N = strlen(txt);
  36.     int i, j;
  37.     int p = 0;  // hash value for pattern
  38.     int t = 0; // hash value for txt
  39.     int h = 1;
  40.  
  41.     // The value of h would be "pow(d, M-1)%q"
  42.     for (i = 0; i < M-1; i++)
  43.         h = (h*d)%q;
  44.  
  45.     // Calculate the hash value of pattern and first window of text
  46.     for (i = 0; i < M; i++)
  47.     {
  48.         p = (d * p + pat[i]) % q;
  49.         t = (d * t + txt[i]) % q;
  50.     }
  51.  
  52.     // Slide the pattern over text one by one
  53.     for (i = 0; i <= N - M; i++)
  54.     {
  55.  
  56.         // Check the hash values of current window of text and pattern
  57.         // If the hash values match then only check for characters on by one
  58.         if ( p == t )
  59.         {
  60.             /* Check for characters one by one */
  61.             for (j = 0; j < M; j++)
  62.             {
  63.                 if (txt[i + j] != pat[j])
  64.                     break;
  65.             }
  66.             if (j == M)  // if p == t and pat[0...M-1] = txt[i, i+1, ...i+M-1]
  67.             {
  68.                 printf("Pattern found at index %d \n", i);
  69.             }
  70.         }
  71.  
  72.         // Calculate hash value for next window of text: Remove leading digit,
  73.         // add trailing digit
  74.         if ( i < N - M )
  75.         {
  76.             t = (d * (t - txt[i] * h) + txt[i + M]) % q;
  77.  
  78.             // We might get negative value of t, converting it to positive
  79.             if (t < 0)
  80.                 t = (t + q);
  81.         }
  82.     }
  83. }
  84.  
  85. int main()
  86. {
  87.     vector<char> txt;
  88.     vector<char> pat;
  89.     cout<<"Enter Text:";
  90.     get_input(txt);
  91.     cout<<"Enter Pattern to Search:";
  92.     get_input(pat);
  93.     char *text,*pattern;
  94.     text=&txt[0];
  95.     text[txt.size()]='\0';
  96.     pattern=&pat[0];
  97.     pattern[pat.size()]='\0';
  98.     int q = 101;  // A prime number
  99.     search(pattern, text, q);
  100.     getchar();
  101.     return 0;
  102. }

 

 
Output:
Enter Text:Mr. Antony saw an Ant on Aunt's hand.
Enter Pattern to Search:Ant
Pattern found at index 4
Pattern found at index 18