Advertisement
juanjo12x

Rabin_Karp_1_ocurrencia

Nov 16th, 2014
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.29 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #include <cstring>
  5. #include <string>
  6. #include <cctype>
  7. #include <stack>
  8. #include <bitset>
  9. #include <queue>
  10. #include <list>
  11. #include <vector>
  12. #include <map>
  13. #include <set>
  14. #include <iterator>
  15. #include <sstream>
  16. #include <stdlib.h>
  17. #include <cmath>
  18. #define FOR(i,A) for(typeof (A).begin() i = (A).begin() ; i != (A).end() ; i++)
  19. #define f(i,a,b) for(int i = a ; i < b ; i++)
  20. #define fd(i,a,b) for(int i = a ; i >= b ; i--)
  21. #define debug( x ) cout << #x << " = " << x << endl
  22. #define clr(v,x) memset( v, x , sizeof v )
  23. #define all(x) (x).begin() , (x).end()
  24. #define mp make_pair
  25. #define rall(x) (x).rbegin() , (x).rend()
  26. #define PI acos( -1.0 )
  27. #define EPS 1E-9
  28. #define TAM 100010
  29. #define d 256
  30.  
  31. using namespace std;
  32.  
  33. typedef pair<int,int> ii ;
  34. typedef long long ll ;
  35. typedef long double ld ;
  36. typedef pair<int,ii> pii ;
  37. typedef vector <ii> vii;
  38. typedef vector<int> vi;
  39. void Rabin_Karp(string source,string pattern, int q){
  40.  int n=source.size();int m=pattern.size();
  41.  int p=0;                       // hash value for pattern
  42.  int t=0;               // hash value for text
  43.  int h=1;                       // d^(m-1) value
  44.  // Calculo el valor de d^(m-1) --> Pre-procesamiento en O(n)
  45.  f(i,0,m-1){
  46.     h=(d*h)%q;
  47.  }
  48.  // Multiply each character by its place value and obtain the hash value
  49.  // Initial Hash values of current sliding text window and the pattern is being calculated
  50.   f(i,0,m){
  51.    p=(d*p+pattern[i])%q;
  52.    t=(d*t+source[i])%q;
  53.   }
  54.  
  55.   f(i,0,n-m){
  56.  
  57.     // Check if the current sliding window of text and pattern have same hash values
  58.  
  59.     if(t==p){
  60.     // Check if all characters are same or it's a SPURIOUS HIT !
  61.     int index=0;
  62.     f(j,0,m){
  63.         if(source[i+j]!=pattern[j]) {
  64.         break;
  65.         }
  66.         index++;
  67.     }
  68.     if(index==m) cout<<"Pattern matched at index "<<i<<endl;
  69.     }
  70.     // Now compute the next sliding window for the text using previous value..
  71.     if(i<n-m){
  72.     t = (d*(t - source[i]*h) + source[i+m])%q;
  73.      
  74.     // We might get negative value of t, converting it to positive
  75.      
  76.     if(t < 0)
  77.     t = (t + q);
  78.     }
  79.  }
  80. }
  81. int main() {
  82.      string source;string pattern;int q;
  83.      cin>>source;cin>>pattern;
  84.      scanf("%d",&q);
  85.      debug(source);debug(pattern);
  86.      Rabin_Karp(source,pattern,q);
  87.     return 0;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement