Cod sursa(job #530964)

Utilizator ChallengeMurtaza Alexandru Challenge Data 8 februarie 2011 18:43:41
Problema Abc2 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.82 kb
#include <fstream>
#include <cstring>
#include <vector>

using namespace std;

const char InFile[]="abc2.in";
const char OutFile[]="abc2.out";
const unsigned int MaxN=10111000;
const unsigned int MaxL=50;
const unsigned int MOD=666013;

ifstream fin(InFile);
ofstream fout(OutFile);

struct s_el
{
	unsigned int cod;
	unsigned int count;
};

char str[MaxN],cuv[MaxL];
unsigned int N,L,sol,cpow,cod;
vector<s_el> H[MOD];

inline void add(unsigned int key)
{
	unsigned int hash=key%MOD;
	vector<s_el>::iterator it;
	for(it=H[hash].begin();it!=H[hash].end();++it)
	{
		if(it->cod==key)
		{
			break;
		}
	}
	if(it!=H[hash].end())
	{
		++it->count;
	}
	else
	{
		s_el el;
		el.cod=key;
		el.count=1;
		H[hash].push_back(el);
	}
}

inline unsigned int get(unsigned int key)
{
	unsigned int hash=key%MOD;
	vector<s_el>::iterator it;
	for(it=H[hash].begin();it!=H[hash].end();++it)
	{
		if(it->cod==key)
		{
			break;
		}
	}
	if(it!=H[hash].end())
	{
		unsigned int t=it->count;
		it->count=0;
		return t;
	}
	return 0;
}

int main()
{
	fin>>str;
	fin>>cuv;
	N=strlen(str);
	L=strlen(cuv);
	cod=0;
	for(register unsigned int i=0;i<L;++i)
	{
		str[i]-='a';
		cod*=3;
		cod+=str[i];
	}
	cpow=1;
	for(register unsigned int i=1;i<L;++i)
	{
		cpow*=3;
	}
	add(cod);
	for(register unsigned int i=L;i<N;++i)
	{
		str[i]-='a';
		cod-=cpow*str[i-L];
		cod*=3;
		cod+=str[i];
		add(cod);
	}
	while(!fin.eof())
	{
		cod=0;
		for(register unsigned int i=0;i<L;++i)
		{
			cod*=3;
			cod+=cuv[i]-'a';
		}
		sol+=get(cod);
		memset(cuv,0,sizeof(cuv));
		fin>>cuv;
	}
	fin.close();

	cod=0;
	for(register unsigned int i=0;i<L;++i)
	{
		cod*=3;
		cod+=cuv[i]-'a';
	}
	sol+=get(cod);
	memset(cuv,0,sizeof(cuv));
	fin>>cuv;

	fout<<sol;
	fout.close();
	return 0;
}