Cod sursa(job #2728332)

Utilizator om6gaLungu Adrian om6ga Data 23 martie 2021 04:43:22
Problema Hashuri Scor 70
Compilator c-32 Status done
Runda Arhiva educationala Marime 1.73 kb
#include <stdio.h>
#include <string.h>

int i, j, N;
unsigned int *hashtable[512];

unsigned int lowbias32(unsigned int x) {
    x ^= x >> 16;
    x *= 0x7feb352d;
    x ^= x >> 15;
    x *= 0x846ca68b;
    x ^= x >> 16;
    return x;
}

void op_add(unsigned int index, unsigned int n) {
	//printf("i = %d, index = %d, size = %d\n", i, index, hashtable[index][0]);
	for (j = 1; j < hashtable[index][0]; j++) {
		//printf("    j = %d\n", j);
		if (0 == hashtable[index][j]) {
			hashtable[index][j] = n;
			return;
		}
	}
	if (j == hashtable[index][0]) {
		//printf("index %d resizing from %d to %d\n", index, hashtable[index][0], j<<1);
		hashtable[index] = realloc(hashtable[index], j<<3);
		memset(hashtable[index]+j, 0, j);
		hashtable[index][0] <<= 1;
	}
}

void op_remove(unsigned int index, unsigned int n) {
	for (j = 1; j < hashtable[index][0]; j++) {
		if (n == hashtable[index][j]) {
			hashtable[index][j] = 0;
			return;
	    }
	}
}

void op_question(unsigned int index, unsigned int n) {
	for (j = 1; j < hashtable[index][0]; j++) {
		if (n == hashtable[index][j]) {
			printf("1\n");
			return;
		}
	}
	if (j == hashtable[index][0]) {
		printf("0\n");
	}
}

void main() {
	unsigned int n, o, flag;
	freopen("hashuri.in", "r", stdin);
	freopen("hashuri.out", "w", stdout);
	unsigned int index;
	void *op_fct[] = {op_add, op_remove, op_question};
	
	for (i = 0; i < 512; i++) {
		hashtable[i] = calloc(1300, sizeof(unsigned int));
		hashtable[i][0] = 1300;
	}   
	
	scanf("%d", &N);
	i = N;
	while (1) {
		i--;
		scanf("%u %u", &o, &n);
		index = (lowbias32(n) & 0x1ff);
		((void (*)(unsigned int, unsigned int))op_fct[o-1])(index, n);
		if (i == 0)
	        break;
	}

	return;
}