Cod sursa(job #2728338)

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

int i, j, N;


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

struct bucket {
	unsigned int capacity;
	unsigned int count;
	unsigned int *members;
};

typedef struct bucket bucket;
bucket *hashtable[512];

void op_add(unsigned int index, unsigned int n) {
	bucket *b = hashtable[index];
	
	for (j = 0; j < b->count; j++) {
		if (n == b->members[j])
		    return;
	}
	if (b->count == b->capacity) {
		b->members = realloc(b->members, b->capacity<<3);
		b->members[b->count++] = n;
		b->capacity <<= 1;
		return;
	}
	b->members[j] = n;
	b->count++;
}
void op_remove(unsigned int index, unsigned int n) {
	bucket *b = hashtable[index];
	
	for (j = 0; j < b->count; j++) {
		if (n == b->members[j]) {
			if (j != b->count - 1) {
				memmove(b->members + j, b->members + j + 1, (b->count - j - 1)<<2);
			}
			b->count--;
			return;
	    }
	}
}
void op_question(unsigned int index, unsigned int n) {
	bucket *b = hashtable[index];

	for (j = 0; j < b->count; j++) {
		if (n == b->members[j]) {
			printf("1\n");
			return;
		}
	}
	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] = malloc(sizeof(bucket));
		hashtable[i]->members = malloc(1300<<2);
		hashtable[i]->capacity = 1300;
		hashtable[i]->count = 0;
	}   
	
	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;
}