Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<string>
- #include <vector>
- using namespace std;
- int M = 31;
- int P = 40009;
- unsigned int hash_p(string &p, int P, int M)
- {
- unsigned int h_p = 0;
- for (int i = 0; i < p.length(); i++)
- h_p = (h_p*P + p[i]) % M;
- return h_p;
- }
- int p(string &p, int P, int M)
- {
- int Q = 1;
- for (int i = 0; i < p.length(); i++)
- Q = (Q * P) % M;
- return Q;
- }
- int find(string &s, string &p, int Q, int P, int M, int h, int h_p)
- {
- for (int i = 0; i <int(s.length() - p.length()); i++)
- { if (h == h_p)
- return 1;
- else if (i < int(s.length() - p.length() - 1))
- h = (((h - Q*s[i]) % M + M) % M * P + s[i + p.length()]) % M;
- if (h == h_p)
- return i + 1;
- return -1;
- }
- }
- int main()
- {
- string s, p;
- getline(cin, p);
- getline(cin, s);
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement