Pagini recente » Istoria paginii utilizator/andreidebelii | Cod sursa (job #3359831)
#include <iostream>
#include <algorithm>
#include <vector>
#define MOD 666013
#define NMAX MOD
int hash(int x)
{
return x % MOD;
}
void hash_table_insert(std::vector<int> *hash_table, int x)
{
std::vector<int> &bucket = hash_table[hash(x)];
if (std::find(bucket.begin(), bucket.end(), x) != bucket.end())
return;
bucket.push_back(x);
}
void hash_table_delete(std::vector<int> *hash_table, int x)
{
std::vector<int> &bucket = hash_table[hash(x)];
auto it = std::find(bucket.begin(), bucket.end(), x);
if (it != bucket.end())
bucket.erase(it);
}
bool hash_table_search(std::vector<int> *hash_table, int x)
{
const std::vector<int> &bucket = hash_table[hash(x)];
return std::find(bucket.begin(), bucket.end(), x) != bucket.end();
}
int main()
{
int n;
std::vector<int> hash_table[NMAX];
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, x);
break;
case 2:
hash_table_delete(hash_table, x);
break;
case 3:
if (hash_table_search(hash_table, x))
std::cout << 1 << '\n';
else
std::cout << 0 << '\n';
break;
}
}
return 0;
}