Cod sursa(job #2869383)

Utilizator QwertyDvorakQwerty Dvorak QwertyDvorak Data 11 martie 2022 14:53:31
Problema Potrivirea sirurilor Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.73 kb
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define mp make_pair
#define dbg(x) cout << #x <<": " << x << "\n";
using ll = long long;

class InP {

	FILE *fin;
	char *buff;
	int sp;
public:
	InP(const char *p) {
		fin = fopen(p, "r");
		buff = new char[4096]();
		sp = 4095;
	}

	char read_ch() {
		sp++;
		if (sp == 4096) {
			fread(buff, 1, 4096, fin);
			sp = 0;
		}
		return buff[sp];
	}

	InP& operator >> (int &n) {

		char c;
		int sgn = 1;
		while (!isdigit(c = read_ch()) && c != '-');

		if (c == '-') {
			n = 0;
			sgn = -1;
		}
		else n = c - '0';
		while (isdigit(c = read_ch()))
			n = n * 10 + c - '0';
		n = n * sgn;
		return *this;
	}

	InP& operator >> (ll &n) {

		char c;
		ll sgn = 1;
		while (!isdigit(c = read_ch()) && c != '-');

		if (c == '-') {
			n = 0;
			sgn = -1;
		}
		else n = c - '0';
		while (isdigit(c = read_ch()))
			n = n * 10 + c - '0';
		n = n * sgn;
		return *this;
	}


};




class OuP {

	FILE *fout;
	char *buff;
	int sp;

public:
	OuP(const char *p) {
		fout = fopen(p, "w");
		buff = new char[50000]();
		sp = 0;
	}
	~OuP() {
		fwrite(buff, 1, sp, fout);
		fclose(fout);
	}

	void write_ch(char c) {
		if (sp == 50000) {
			fwrite(buff, 1, sp, fout);
			sp = 0;
		}
		buff[sp++] = c;
	}

	OuP& operator << (int n) {
		if (n <= 9)
			write_ch(n + '0');
		else {
			*this << (n / 10);
			write_ch(n % 10 + '0');
		}
		return *this;
	}

	OuP& operator << (ll n) {
		if (n <= 9)
			write_ch(n + '0');
		else {
			*this << (n / 10);
			write_ch(n % 10 + '0');
		}
		return *this;
	}

	OuP &operator <<(char c) {
		write_ch(c);
		return *this;
	}
	OuP &operator << (const char *p) {
		while (*p) {
			write_ch(*p);
			p++;
		}
		return *this;
	}

};


// InP fin("inversmodular.in");
// OuP fout("inversmodular.out");

ifstream fin("strmatch.in");
ofstream fout("strmatch.out");

string pat, s;
string t;
vector<int> z;
int main() {

	fin >> pat >> s;
	t = pat + "$" + s;
	int n = t.size();
	z = vector<int>(n, 0);
	int st, dr;
	st = dr = 0;
	for (int i = 1; i < n; ++i) {
		if (i > dr) {
			st = dr = i;
			while (dr < n && t[dr] == t[dr - st])
				dr++;
			dr--;
			z[i] = dr - st + 1;
		}
		else {
			if (z[i - st] < dr - i + 1)
				z[i] = z[i - st];
			else {
				st = i;
				while (dr < n && t[dr] == t[dr - st])
					dr++;
				dr--;
				z[i] = dr - st + 1;
			}
		}
	}

	int ans = 0;
	vector<int> poz;
	for (int i = 0; i < n; ++i)
		if (z[i] == pat.size()) {
			ans++;
			if (ans <= 1000)
				poz.pb(i - pat.size() - 1);
		}

	fout << ans << '\n';
	for (auto i : poz)
		fout << i << ' ';

	// fin.close();
	// fout.close();
	return 0;
}