Pagini recente » Monitorul de evaluare | Cod sursa (job #3248138) | Cod sursa (job #3361209) | Cod sursa (job #1504996) | Cod sursa (job #3359829)
#include <iostream>
#include <algorithm>
#define MOD 1000003
#define BUCKET_SIZE 2000
#define NMAX MOD
int hash_table_sizes[NMAX] = {0};
int hash_table[NMAX][BUCKET_SIZE] = {0};
int hash(int x)
{
return x % MOD;
}
void hash_table_insert(int (*hash_table)[BUCKET_SIZE], int *hash_table_sizes, int x)
{
int &size = hash_table_sizes[hash(x)];
int (&bucket)[BUCKET_SIZE] = hash_table[hash(x)];
for (int i = 0; i < size; ++i)
if (bucket[i] == x)
return;
bucket[size++] = x;
}
void hash_table_delete(int (*hash_table)[BUCKET_SIZE], int *hash_table_sizes, int x)
{
int &size = hash_table_sizes[hash(x)];
int (&bucket)[BUCKET_SIZE] = hash_table[hash(x)];
for (int i = 0; i < size; ++i)
if (bucket[i] == x) {
--size;
for (int j = i; j < size; ++j)
bucket[j] = bucket[j + 1];
return;
}
}
bool hash_table_search(int (*hash_table)[BUCKET_SIZE], int *hash_table_sizes, int x)
{
int &size = hash_table_sizes[hash(x)];
int (&bucket)[BUCKET_SIZE] = hash_table[hash(x)];
for (int i = 0; i < size; ++i)
if (bucket[i] == x)
return true;
return false;
}
int main()
{
int n;
freopen("hashuri.in", "r", stdin);
freopen("hashuri.out", "w", stdout);
std::cin >> n;
for (int op, x, i = 0; i < n; ++i)
{
std::cin >> op >> x;
switch (op) {
case 1:
hash_table_insert(hash_table, hash_table_sizes, x);
break;
case 2:
hash_table_delete(hash_table, hash_table_sizes, x);
break;
case 3:
if (hash_table_search(hash_table, hash_table_sizes, x))
std::cout << 1;
else
std::cout << 0;
break;
}
}
return 0;
}