Cod sursa(job #629564)

Utilizator ContraPunctContrapunct ContraPunct Data 3 noiembrie 2011 15:20:39
Problema Potrivirea sirurilor Scor 14
Compilator cpp Status done
Runda Arhiva educationala Marime 1.81 kb
#include<fstream>
#include <iostream>
#include <cstdio>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;

const int B = 67;
const int P = 666013;
const int BB = 91;
const long PP = 1000007;

string a,b;

//vector<int> Bp;
//vector<int> BBp;
int Bp = 1, BBp = 1;
vector<int> poz;
int x;
void ReadData()
{
	ifstream fin("strmatch.in");
	fin>>a>>b;
	
}
void Baza()
{
	int k = a.length(), nr;
	//Bp.push_back(1);
	//BBp.push_back(1);
	for(int i=1;i<k;i++)
	{
		//nr = (long long) Bp[i-1] * B % P;
		//Bp.push_back(nr);
		Bp = Bp * B % P;
		//nr = (long long) BBp[i-1] * BB %PP;
		//BBp.push_back(nr);
		//BBp = BBp * BB % PP;
	}
}
void Solve()
{
	int k = a.length(),i;
	int hashb=0,hasha=0;
	int hashb1=0, hasha1=0;
	for (i=0; i < k; i++)
	{
		hasha  = (hasha * B + a[i]) % P;
		//hasha1 = (hasha1 * BB + a[i]) % PP;
	}
	/*for (i=0; i < k; i++)
	{
		hashb  = (hashb * B + b[i]) % P;
		//hashb1 = (hashb1 * BB + b[i]) % PP;
	}
	int ok = 0;
	if( hasha == hashb)
	{
		//poz.push_back(0);
		for(i=0;i<k ;i++)
			if(a[i]!=b[i])
				ok = 1;
		if( ok == 0 )
		{
			poz.push_back(0);
		}
	}*/
	int j = b.length();
	for(i=0; (i< j);++i)
	{
		hashb  = ((P + hashb - (i >= k)? (b[i-k] * Bp % P) : 0) * B  + b[i])%P;
		//hashb1 = ((PP + hashb1 - (long long) b[i-k] * BBp % PP) * BB + b[i])%PP;
		if( hasha == hashb && i >= k - 1)
		{
		//verific;
			//poz.push_back(i-k+1);
			int ok = 0;
			for(int q = 0; q<k; ++q )
				if(a[q] != b[i-k+q+1])
					ok = 1;
			if( !ok)
				poz.push_back(i-k+1);
		}
	}

}
//P = 10 la a 16
void Afisare()
{
	ofstream fout("strmatch.out");
	int i;
	fout<<poz.size()<<"\n";
	for(i=0;i < min(1000, (int) poz.size());i++)
		fout<<poz[i]<<" ";
}
int main()
{
	ReadData();
	Baza();
	Solve();
	Afisare();
	return 0;
}