Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- #include <algorithm>
- using namespace std;
- int main()
- {
- string s = "the", t = "We implement the algorithm";
- const int p = 31;
- vector<long long> p_pow(max(s.size(), t.size()));
- p_pow[0] = 1;
- for (size_t i = 1; i<p_pow.size(); ++i) p_pow[i] = p_pow[i - 1] * p; //
- for (size_t i = 0; i<p_pow.size(); ++i) cout << p_pow[i] << endl;
- vector<long long> h(t.size());
- for (size_t i = 0; i<t.size(); ++i)
- {
- h[i] = (t[i] - 'a' + 1) * p_pow[i];
- if (i) h[i] += h[i - 1];
- }
- for (size_t i = 1; i<h.size(); ++i) cout << endl << h[i];
- long long h_s = 0;
- for (size_t i = 0; i<s.size(); ++i) h_s += (s[i] - 'a' + 1) * p_pow[i];
- cout << endl << h_s << endl;
- for (size_t i = 0; i + s.size() - 1 < t.size(); ++i)
- {
- long long cur_h = h[i + s.length() - 1];
- if (i) cur_h -= h[i - 1];
- if (cur_h == h_s * p_pow[i])
- cout << i << ' ';
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement