Cod sursa(job #101310)

Utilizator andrei.12Andrei Parvu andrei.12 Data 13 noiembrie 2007 12:50:48
Problema Abc2 Scor 0
Compilator cpp Status done
Runda Happy Coding 2007 Marime 1.29 kb
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char w[10000000], c, s[20],*pp;
int x, i, nr, ind, lg, z;
unsigned int sol, r, v[50005], pt[21], cnt, tr[50005], t;
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 cnt){
	int li = 0, ls = t, x;
	while (li <= ls){
		x = (li+ls) / 2;
		if (cnt == tr[x])
			return 1;
		else
			if (cnt < tr[x])
				ls = x-1;
			else
				li = x+1;
	}
	return 0;
}
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);
	for (i=1; i<=ind; i++)
		if (v[i] != v[i+1])
			tr[++t] = v[i];
	cnt = 0;
	for (i=0; i<nr; i++){
		x = w[i]-'a';
		cnt += pt[nr-i-1]*x;
	}
	sol += caut(cnt);
	for(pp=w+nr;*pp!='\n';++pp){
		x=*pp-'a';
		cnt = (cnt - pt[nr-1]*(*(pp-nr)-'a')) * 3 + x;
		sol += caut(cnt);
	}
	printf("%u\n", sol);
	fclose(stdin);
	fclose(stdout);
	return 0;
}