Pagini recente » Cod sursa (job #971357) | Cod sursa (job #840902) | Cod sursa (job #3036487) | Cod sursa (job #1391859) | Cod sursa (job #1415871)
#include <stdio.h>
#include <string.h>
#define MAX_N 1000000
#define MOD 0xA299D
#define NIL -1
typedef struct {
int v, next;
} hash;
hash l[MAX_N]; // liste simplu inlantuite alocate static
int lsize;
int h[MOD]; // inceputul listelor alocate static
int st[MAX_N], s; // stiva de pozitii refolosibile
inline bool hashFind(const int &x) {
int i = h[x - (x / MOD) * MOD];
while ((i != NIL) && (l[i].v != x)) {
i = l[i].next;
}
return (i != NIL);
}
inline void hashInsert(const int &x) {
int p = s ? st[--s] : lsize++;
int hash = x - (x / MOD) * MOD;
l[p].v = x;
l[p].next = h[hash];
h[hash] = p;
}
inline void hashExtract(const int &x) {
int hash = x - (x / MOD) * MOD;
int i = h[hash];
if (l[i].v == x) {
h[hash] = l[i].next;
} else {
while ((l[i].next != NIL) && (l[l[i].next].v != x)) {
i = l[i].next;
}
if (l[i].next != NIL) {
st[s++] = l[i].next;
l[i].next = l[l[i].next].next;
}
}
}
int main(void) {
FILE *f, *g;
int testNum;
int x;
memset(h, NIL, MOD << 2);
f = fopen("hashuri.in", "r");
fscanf(f, "%d ", &testNum);
g = fopen("hashuri.out", "w");
for (; testNum; testNum--) {
char c = fgetc(f);
fscanf(f, "%d ", &x);
if (c == '1') {
hashInsert(x);
} else if (c == '2') {
hashExtract(x);
} else {
fputc(hashFind(x) + '0', g);
fputc('\n', g);
}
}
fclose(f);
fclose(g);
return 0;
}