Cod sursa(job #98456)

Utilizator megabyteBarsan Paul megabyte Data 10 noiembrie 2007 13:39:33
Problema Abc2 Scor 0
Compilator cpp Status done
Runda Happy Coding 2007 Marime 1.06 kb
#include <cstdio>
#include <string>
#define INF "abc2.in"
#define OUF "abc2.out"
#define LMAX 10000002
#define HMAX 1046527
#define SG 1
#define GG 2
using namespace std;

char s[LMAX];
char h[HMAX]={0};
int n,m,sz;

inline int mctod(char c)
{
	switch(c)
	{
		case 'a': return 1;
		case 'b': return 2;
		case 'c': return 3;
		default :return 0;
	}
}

void hmap(char *v)
{
	int key=0;
	while(*v)
	{
		key=(key*7+mctod(*v))%HMAX;
		h[key]|=SG;
		++v;
	}
	h[key]|=GG;
}

bool test(char *v)
{
	int key;
	bool ok=true;
	key=0;
	while(*v&&ok)
	{
		key=(key*7+mctod(*v))%HMAX;
		if(!h[key]) ok=false;
		++v;
	}
	return ok;
}

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

	fgets(word,32,in);
	m=strlen(word);--m;
//	printf("%s %s",s,word);
	hmap(word);

	while(fgets(word,32,in)) hmap(word);//printf("%s",word);}

	int i=0,j;
	while(i+m<n)
	{
		for(j=0;j<m;++j) word[j]=s[i+j];
		if(test(word)) ++sol;//printf("%d ",i);}
		++i;
	}

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