Cod sursa(job #2758940)

Utilizator alex.prohnitchiAlex Prohnitchi alex.prohnitchi Data 14 iunie 2021 15:37:32
Problema Potrivirea sirurilor Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.65 kb
// by Alex Prohnitchi
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;

const ll mod=1e9+7;
const ll INF=2e9;

const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};

#define all(x) x.begin(),x.end()
#define allr(x) x.rbegin(),x.rend()
#define rc(x)  return cout<<x<<"\n",0
#define sz(s)  (int) s.size()
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define PI 3.14159265358979

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int rand(int l,int r){return uniform_int_distribution<int>(l,r)(rng);}

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

ll t,n;

void solve() {
	string a,b;
	fin >> a;
	fin >> b;
	ll s1=sz(a),s2=sz(b);
	ll pref[s1+3];
	vector<int>ans;
	pref[0]=-1;
	pref[1]=0;
	ll j=0;
	for (int i=2; i<=s1; i++) {
		if (a[i-1]==a[j]) {
			pref[i]=pref[i-1]+1;
			j++;
		}
		else {
			pref[i]=0;
			j=0;
		}
	}
/*	for (int i=1; i<=s1; i++) {
		cout << pref[i] << " ";
	}*/
	ll t=0;
	j=0;
	while (t<s2) {
		if (a[j]==b[t]) {
			j++;
			t++;
			if (j==s1) {
				ans.pb(t-s1);
				j=pref[j];	
			}
		}
		else {
			j=pref[j];
			if (j<0) {
				t++;
				j++;	
			}		
		}
		//cout << j << " " << t << '\n';
	}
	fout << sz(ans) << '\n';
	for (int i=0; i<min(sz(ans),1000); i++) {
		fout << ans[i] << " ";
	}
	fout << '\n';
	return;
}

int main() {
//ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	t=1;
//	cin >> t;
	while (t--) {
		solve();
	}

}

/*
ABA
CABBCABABAB
*/