Cod sursa(job #2728331)

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

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

void main() {
	int i, j, N;
	unsigned int n, o, flag;
	freopen("hashuri.in", "r", stdin);
	freopen("hashuri.out", "w", stdout);
	unsigned int *hashtable[512];
	unsigned int index;
	
	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);
		switch(o) {
			case 1:
				//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;
						break;
					}
				}
				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;
				}
				break;
			case 2:
				for (j = 1; j < hashtable[index][0]; j++) {
					if (n == hashtable[index][j]) {
						hashtable[index][j] = 0;
						break;
					}
				}
				break;
			case 3:
				for (j = 1; j < hashtable[index][0]; j++) {
					if (n == hashtable[index][j]) {
						printf("1\n");
						break;
					}
				}
				if (j == hashtable[index][0]) {
					printf("0\n");
				}
		}
		if (i == 0)
	        break;
	}

	return;
}