Cod sursa(job #629615)

Utilizator ContraPunctContrapunct ContraPunct Data 3 noiembrie 2011 16:07:47
Problema Potrivirea sirurilor Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 1.12 kb
#include<fstream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;

const int B = 67;
const int P = 666013;

string a,b;

int Bp = 1, BBp = 1;
vector<int> poz;
int x;
int calc[256];
void ReadData()
{
	ifstream fin("strmatch.in");
	fin>>a>>b;
	
}
void Baza()
{
	int k = a.length();
	for(int i=1;i<k;++i)
	{
		Bp = Bp * B % P;
	}
}
void Solve()
{
	int k = a.length(),i;
	int hashb=0,hasha=0;
	for (i=0; i < k; ++i)
	{
		hasha  = (hasha * B + a[i]) % P;
	}
	k--;
	for (i=0; i < k; ++i)
	{
		hashb  = (hashb * B + b[i]) % P;
	}
	k++;
	
	int j = b.length();
	
	for (int i = 0; i <256; ++i)
		calc[i] = (i * Bp) % P;
	
	for(i=k-1; i < j;++i)
	{
		hashb  = ((P + hashb - calc[b[i - k]]) * B + b[i])%P;
		if( hasha == hashb && i >= k - 1)
		{		
			int ok = 0, s=i-k+1;
			for(int q = 0; q<k; ++q )
				if(a[q] != b[s+q])
					ok = 1;
			if( !ok)
				poz.push_back(s);
		}
	}
}

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