Advertisement
Guest User

Untitled

a guest
Dec 12th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.82 KB | None | 0 0
  1. #include<iostream>
  2. #include<string>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. int M = 31;
  8. int P = 40009;
  9.  
  10. unsigned int hash_p(string &p, int P, int M)
  11. {
  12.     unsigned int h_p = 0;
  13.     for (int i = 0; i < p.length(); i++)
  14.         h_p = (h_p*P + p[i]) % M;
  15.     return h_p;
  16. }
  17.  
  18. int p(string &p, int P, int M)
  19. {
  20.     int Q = 1;
  21.     for (int i = 0; i < p.length(); i++)
  22.         Q = (Q * P) % M;
  23.     return Q;
  24. }
  25.  
  26. int find(string &s, string &p, int Q, int P, int M, int h, int h_p)
  27. {
  28.     for (int i = 0; i <int(s.length() - p.length()); i++)
  29.     {   if (h == h_p)
  30.             return 1;
  31.         else if (i < int(s.length() - p.length() - 1))
  32.             h = (((h - Q*s[i]) % M + M) % M * P + s[i + p.length()]) % M;
  33.     if (h == h_p)
  34.         return i + 1;
  35.     return -1;
  36. }
  37. }
  38.  
  39. int main()
  40. {
  41.     string s, p;
  42.     getline(cin, p);
  43.     getline(cin, s);
  44.  
  45.  
  46.  
  47.         system("pause");
  48.     return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement