Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define int long long
- const long long INF = 1e9 + 7;
- const int MAXN = 5e4 + 10;
- const int N = 1e5 + 10;
- const int M1 = 1e9 + 123;
- const int M2 = 1e9 + 321;
- const int P1 = 22811;
- const int P2 = 22699;
- array<int, MAXN> power1, power2;
- void init_pow() {
- power1[0] = 1;
- power2[0] = 1;
- for (int i = 1; i < MAXN; ++i) {
- power1[i] = (power1[i - 1] * P1) % M1;
- power2[i] = (power2[i - 1] * P2) % M2;
- }
- }
- void build_hash(string& s, vector<pair<int, int>>& h) {
- int n = s.size();
- h.resize(n + 1);
- h[0].first = 0;
- h[0].second = 0;
- for (int i = 0; i < n; ++i) {
- h[i + 1].first = (h[i].first + (s[i] - 'a' + 1) * power1[i]) % M1;
- h[i + 1].second = (h[i].second + (s[i] - 'a' + 1) * power2[i]) % M2;
- }
- }
- pair<int, int> get_hash(string& s) {
- int n = s.size();
- pair<int, int> prev, cur;
- prev = {0, 0};
- for (int i = 0; i < n; ++i) {
- cur.first = (prev.first + (s[i] - 'a' + 1) * power1[i]) % M1;
- cur.second = (prev.second + (s[i] - 'a' + 1) * power2[i]) % M2;
- prev = cur;
- }
- return cur;
- }
- bool is_equal(vector<pair<int, int>>& h_l, vector<pair<int, int>>& h_r, int start1, int start2, int len) {
- pair<int, int> d_l, d_r;
- d_l.first = ((h_l[start1 + len].first - h_l[start1].first + M1) * power1[start2]) % M1;
- d_l.second = ((h_l[start1 + len].second - h_l[start1].second + M2) * power2[start2]) % M2;
- d_r.first = ((h_r[start2 + len].first - h_r[start2].first + M1) * power1[start1]) % M1;
- d_r.second = ((h_r[start2 + len].second - h_r[start2].second + M2) * power2[start1]) % M2;
- return d_l == d_r;
- }
- signed main() {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- string a, b;
- cin >> a >> b;
- int n = a.size(), m = b.size();
- if (m > n) {
- return 0;
- }
- vector<pair<int, int>> h_a, h_b;
- init_pow();
- build_hash(a, h_a);
- build_hash(b, h_b);
- for (int i = 0; i <= n - m; ++i) {
- if (is_equal(h_a, h_b, i, 0, m)) {
- cout << i << ' ';
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment