Advertisement
Kaidul

RK search

Dec 26th, 2016
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.60 KB | None | 0 0
  1. // average time: O(n + m), worst case: O(n * m)
  2. // popular application: plagarism check
  3.  
  4. // d is the number of characters in input alphabet
  5. #define d 256
  6.  
  7. void rkSearch(string const& pattern, string const& text) {
  8.     int m = (int)pattern.length();
  9.     int n = (int)text.length();
  10.  
  11.     int q = 101; // some small prime, must be less than INT_MAX
  12.  
  13.     int h = 1;
  14.     // h = pow(d, M - 1) % q
  15.     for(int i = 0; i < m - 1; i++) {
  16.         h = (h * d) % q;
  17.     }
  18.  
  19.     // calculate the hash value of pattern and first window of text
  20.     int pHash = 0;
  21.     int tHash = 0;
  22.     for(int i = 0; i < m; i++) {
  23.         pHash = (d * pHash + pattern[i]) % q;
  24.         tHash = (d * tHash + text[i]) % q;
  25.     }
  26.  
  27.     // slide the pattern over text one by one
  28.     for(int i = 0; i <= n - m; i++) {
  29.  
  30.         // check the hash value of current window of text and pattern
  31.         // If the hash values match then only check for characters one by one
  32.         if(pHash == tHash) {
  33.             int j;
  34.             for(j = 0; j < m; j++) {
  35.                 if(text[i + j] != pattern[j]) {
  36.                     break;
  37.                 }
  38.             }
  39.             if(j == m) {
  40.                 // pattern found at index position i
  41.             }
  42.         }
  43.  
  44.         // calculate hash value of next window of text
  45.         // remove leading digit, add trailing digit
  46.         if(i < n - m) {
  47.             tHash = (d * (tHash - text[i] * h) + text[i + m]) % q;
  48.  
  49.             // incase of getting negative value of tHash after modulo operation
  50.             if(tHash < 0) {
  51.                 tHash += q;
  52.             }
  53.         }
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement