Cod sursa(job #531046)

Utilizator ChallengeMurtaza Alexandru Challenge Data 8 februarie 2011 20:40:21
Problema Abc2 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.27 kb
#include <fstream>
#include <cstring>
#include <algorithm>

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 MaxK=66111;

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

char str[MaxN],cuv[MaxL];
unsigned int N,L,sol,cpow,cod,C[MaxK],K,tstep;

inline unsigned int get(unsigned int key)
{
	int step=tstep,pos;
	for(pos=0;step;step>>=1)
	{
		if(pos+step<=K && C[pos+step]<=key)
		{
			pos|=step;
		}
	}
	return C[pos]==key;
}

int main()
{
	fin.getline(str,sizeof(str));
	N=strlen(str);
	
	K=0;
	while(fin.getline(cuv,sizeof(cuv)))
	{
		if(L==0)
		{
			L=strlen(cuv);
		}

		cod=0;
		for(register unsigned int i=0;i<L;++i)
		{
			cod*=3;
			cod+=cuv[i]-'a';
			cuv[i]=0;
		}
		C[++K]=cod;
	}
	fin.close();
	sort(C+1,C+1+K);
	for(tstep=1;tstep<=K;tstep<<=1);

	cpow=1;
	for(register unsigned int i=1;i<L;++i)
	{
		cpow*=3;
	}

	cod=0;
	for(register unsigned int i=0;i<L;++i)
	{
		str[i]-='a';
		cod*=3;
		cod+=str[i];
	}
	sol+=get(cod);

	for(register unsigned int i=L;i<N;++i)
	{
		str[i]-='a';
		cod-=cpow*str[i-L];
		cod*=3;
		cod+=str[i];
		sol+=get(cod);
	}

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