Cod sursa(job #2886383)

Utilizator QwertyDvorakQwerty Dvorak QwertyDvorak Data 7 aprilie 2022 18:21:20
Problema Potrivirea sirurilor Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.34 kb
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define dbg(x) cout << #x <<": " << x << "\n";
#define sz(x) ((int)x.size())

using ll = long long;

const string fn = "strmatch";
ifstream fin(fn + ".in");
ofstream fout(fn + ".out");

const int mod1 = 666013;
const int mod2 = 1e9 + 7;

string pat, s;
const int base = 67;


int main() {


	fin >> pat >> s;
	int n = sz(pat);
	int m = sz(s);
	pair<ll, ll> patHash = {0, 0};
	pair<ll, ll> acHash = {0, 0};
	pair<ll, ll> fPwr = {1, 1};
	for (int i = 0; i < n; ++i)
		patHash = {
		(patHash.first * base + pat[i]) % mod1,
		(patHash.second * base + pat[i]) % mod2
	};

	for (int i = 1; i < n; ++i)
		fPwr = {
		fPwr.first * base % mod1,
		fPwr.second * base % mod2
	};

	for (int i =  0; i < n; ++i)
		acHash = {
		(acHash.first * base + s[i]) % mod1,
		(acHash.second * base + s[i]) % mod2
	};
	vector<int> ans;
	if (acHash == patHash) {
		ans.pb(0);
	}
	for (int i = n; i < m; ++i) {

		acHash = {
			((acHash.first - fPwr.first * s[i - n] % mod1 + mod1) * base + s[i]) % mod1,
			((acHash.second - fPwr.second * s[i - n] % mod2 + mod2) * base + s[i]) % mod2

		};

		if (acHash == patHash) {
			ans.pb(i - n + 1);
		}
	}

	fout << sz(ans) << '\n';
	for (int i = 0; i < (min(sz(ans), 1000)); ++i)
		fout << ans[i] << " ";

	return 0;
}