Pagini recente » Cod sursa (job #1916013) | Cod sursa (job #2039525) | Cod sursa (job #3201022) | Cod sursa (job #759169) | Cod sursa (job #2728338)
#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;
}