Cod sursa(job #101321)

Utilizator andrei.12Andrei Parvu andrei.12 Data 13 noiembrie 2007 13:06:45
Problema Abc2 Scor 0
Compilator cpp Status done
Runda Happy Coding 2007 Marime 1.28 kb
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char w[10000000], c, s[20];
int x, i, nr, ind, lg;
unsigned int sol, r, v[50005], pt[21], cnt, z;
int cmp(const void*a, const void*b){
	unsigned int x  = *(unsigned int *)a, y = *(unsigned int*)b;
	if (x < y) return -1;
	if (x > y) return 1;
	return 0;
}
int caut(unsigned int val)  
{  
	int i, step;  
	for (step = 1; step < ind; step <<= 1);  
	for (i = 0; step; step >>= 1)  
		if (i + step < ind && v[i + step] <= val)  
			i += step;  
	return i;  
}  
int main()
{
	freopen("abc2.in", "rt", stdin);
	freopen("abc2.out", "wt", stdout);
	fgets(w, 10000005, stdin);
	pt[0] = 1;
	for (i=1; i<=20; i++)
		pt[i] = 3*pt[i-1];
	ind = -1;
	while (!feof(stdin)){
		fgets(s, 25, stdin);
		nr = strlen(s) - 1;
		r = 0;
		for (i=0; i<nr; i++){
			x = s[i]-'a';
			r += x*pt[nr-i-1];
		}
		v[++ind] = r;
	}
	qsort(v, ind+1, sizeof(v[0]), cmp);
	lg = strlen(w);
	lg --;
	cnt = 0;
	for (i=0; i<nr; i++){
		x = w[i]-'a';
		cnt += pt[nr-i-1]*x;
	}
	z = caut(cnt);
	if (cnt == v[z]) 
		sol ++;
	for (i=nr; i<lg; i++){
		x = w[i]-'a';
		cnt = (cnt - pt[nr-1]*(w[i-nr]-'a')) * 3 + x;
		z = caut(cnt);
		if (v[z] == cnt)
			sol ++;
	}
	printf("%u\n", sol);
	fclose(stdin);
	fclose(stdout);
	return 0;
}