Cod sursa(job #2880563)

Utilizator NFJJuniorIancu Ivasciuc NFJJunior Data 29 martie 2022 21:16:50
Problema Potrivirea sirurilor Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.21 kb
#include <bits/stdc++.h>
using namespace std;
ifstream f("strmatch.in");
ofstream g("strmatch.out");
#define cin f
#define cout g 
const int P = 73;
const int MOD1 = 100007;
const int MOD2 = 100021;

string A, B;

int main()
{
	cin >> A >> B;
	int lenA = A.size(), lenB = B.size();
	if(lenA > lenB)
	{
		cout<<0<<'\n';
		return 0;
	}
	int hashA1, hashA2, P1, P2;
	hashA1 = hashA2 = 0;
	P1 = P2 = 1;
	for(int i=0;i<lenA;i++)
	{
		hashA1 = (hashA1 * P + A[i]) % MOD1;
		hashA2 = (hashA2 * P + A[i]) % MOD2;
		if(i != 0)
		{
			P1 = (P1 * P) % MOD1;
			P2 = (P2 * P) % MOD2;
		}
	}
	int hash1, hash2, k, poz[1001];
	hash1 = hash2 = 0;
	k = 0;
	for(int i=0;i<lenA;i++)
	{
		hash1 = (hash1 * P + B[i]) % MOD1;
		hash2 = (hash2 * P + B[i]) % MOD2;
	}
	if(hash1 == hashA1 and hash2 == hashA2)
		poz[++ k] = 0;
	for(int i=lenA;i<lenB;i++)
	{
		hash1 = ((hash1 - (B[i - lenA] * P1) % MOD1 + MOD1) * P + B[i]) % MOD1;
		hash2 = ((hash2 - (B[i - lenA] * P2) % MOD2 + MOD2) * P + B[i]) % MOD2;
		if(hash1 == hashA1 and hash2 == hashA2)
		{
			poz[++ k] = i - lenA + 1;
			if(k == 1000)
				break;
		}
	}
	cout<<k<<'\n';
	for(int i=1;i<=k;i++)
		cout<<poz[i]<<" ";
	cout<<'\n';
	return 0;
}