Cod sursa(job #415887)

Utilizator slayer4uVictor Popescu slayer4u Data 11 martie 2010 22:07:00
Problema Potrivirea sirurilor Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 2.28 kb
#include <stdio.h>
#include <string>
//#include <iostream>
//#include <fstream>
//#include <string>
//#include <vector>
//using namespace std;

//string a, b;
//vector <int> rez;
char a[2000001], b[2000001], rez[2000001];

long num, nr, p1 = 97, win1 = 15921, p2 = 17, win2 = 21443, alen, blen, b_hash1, a_hash1, rez_size, def_power1, power1, power2, def_power2, a_hash2, b_hash2;

int main ()
{
	freopen ("strmatch.in", "rt", stdin);
	freopen ("strmatch.out", "wt", stdout);
	//ifstream f("strmatch.in");
	//ofstream g("strmatch.out");

	//f>>a;
	//f>>b;
	scanf("%s %s", a, b);

	alen = strlen(a);//a.size();
	blen = strlen(b);//b.size();
	power1 = 1;
	for (long i = alen - 1; i > 0; --i)
	{
		a_hash1 = (a_hash1 + a[i] * power1) % win1;
		b_hash1 = (b_hash1 + b[i] * power1) % win1;
		power1 = (power1 * p1) % win1;
	}
	a_hash1 = (a_hash1 + a[0] * power1) % win1;
	b_hash1 = (b_hash1 + b[0] * power1) % win1;
	def_power1 = power1;

	power2 = 1;
	for (long i = alen - 1; i > 0; --i)
	{
		a_hash2 = (a_hash2 + a[i] * power2) % win2;
		b_hash2 = (b_hash2 + b[i] * power2) % win2;
		power2 = (power2 * p2) % win2;
	}
	a_hash2 = (a_hash2 + a[0] * power2) % win2;
	b_hash2 = (b_hash2 + b[0] * power2) % win2;
	def_power2 = power2;

	for (long i = alen; i < blen; ++i)
	{
		if (a_hash1 == b_hash1 && a_hash2 == b_hash2)
		{
			rez[i - alen] = 1, ++num;
		}

		b_hash1 = ((b_hash1 - (def_power1 * b[i - alen]) % win1 + win1) * p1 + b[i]) % win1;
		b_hash2 = ((b_hash2 - (def_power2 * b[i - alen]) % win2 + win2) * p2 + b[i]) % win2;


		/*
		b_hash1 += (win1 - (def_power1 * b[i - alen] % win1));
		b_hash1 %= win1;
		if (b_hash1 < 0) 
			b_hash1 += win1;
		b_hash1 *= p1;
		b_hash1 += b[i];
		b_hash1 %= win1;

		b_hash2 += (win2 - (def_power2 * b[i - alen] % win2));
		b_hash2 %= win2;
		if (b_hash2 < 0) 
			b_hash2 += win2;
		b_hash2 *= p2;
		b_hash2 += b[i];
		b_hash2 %= win2;*/
	}

	if (a_hash1 == b_hash1 && a_hash2 == b_hash2)
	{
		rez[blen - alen] = 1, ++num;
	}

//	printf("%ld\n", rez.size());
	//g<<num<<endl;
	printf("%ld\n", num);
	//rez.size();
	for (long i = 0; i < blen && nr < 1000; ++i)
//		printf("%ld ", rez[i] - alen);
		if (rez[i])
			//g<<i<<" ", ++nr;
			printf("%ld ", i), ++nr;
	//g<<endl;
	printf("\n");
	return 0;
}