Cod sursa(job #99171)

Utilizator megabyteBarsan Paul megabyte Data 10 noiembrie 2007 22:57:34
Problema Abc2 Scor 0
Compilator cpp Status done
Runda Happy Coding 2007 Marime 1.07 kb
#include <cstdio>
#include <string>
#include <vector>
#define INF "abc2.in"
#define OUF "abc2.out"
#define LMAX 10000002
#define HMAX 1046527
#define SG 1
#define GG 2
#define sz(x) x.size()
#define pb(x) push_back(x)
using namespace std;

char s[LMAX];
int n,m,sz;
vector<long long> h[HMAX];
inline int mctod(char c)
{
	switch(c)
	{
		case 'a': return 1;
		case 'b': return 2;
		case 'c': return 3;
		default :return 0;
	}
}

inline long long hmap(char *v)
{
	long long key=0;
	while(*v)
	{
		key=key*4+mctod(*v);
		++v;
	}
	return key;
}

int main()
{
	FILE *in,*out;
	in=fopen(INF,"r");
	out=fopen(OUF,"w");
	int sol=0,i,j,ind;
	char word[32]={0};
	long long k;
	fgets(s,LMAX,in);
	n=strlen(s);--n;

	fgets(word,32,in);
	m=strlen(word);--m;
	k=hmap(word);
	h[k%HMAX].pb(k);
	while(fgets(word,32,in)) {k=hmap(word);h[k%HMAX].pb(k);}
	
	i=0;
	while(i+m<n)
	{
		for(j=0;j<m;++j) word[j]=s[i+j];
		k=hmap(word);
		ind=k%HMAX;
		if(!h[ind].empty())
		{
			for(j=0;j<sz(h[ind]);++j)
				if(k==h[ind][j]) { ++sol;break;}
		}
		++i;
	}

	fprintf(out,"%d",sol);
	fclose(in);fclose(out);
	return 0;
}