Pagini recente » Cod sursa (job #2285850) | Cod sursa (job #2857798) | Cod sursa (job #1683582) | Cod sursa (job #2313120) | Cod sursa (job #643075)
Cod sursa(job #643075)
#include <iostream>
#include <fstream>
#include <vector>
#define M 600000
using namespace std;
vector<int> hash[M];
int hashFunc(int x)
{
return x % M;
}
void insert(int x)
{
int poz = hashFunc(x);
if (hash[poz].size() > 0)
{
for (unsigned int i = 0; i < hash[poz].size(); i++)
if (hash[poz][i] == x)
return;
}
hash[poz].push_back(x);
}
void del(int x)
{
int poz = hashFunc(x);
if (hash[poz].size() > 0)
{
for (unsigned int i = 0; i < hash[poz].size(); i++)
{
if (hash[poz][i] == x)
{
hash[poz].erase(hash[poz].begin() + i);
}
}
}
}
int find(int x)
{
int poz = hashFunc(x);
if (hash[poz].size() > 0)
{
for (unsigned int i = 0; i < hash[poz].size(); i++)
{
if (hash[poz][i] == x)
{
return 1;
}
}
}
return 0;
}
int main()
{
FILE *fin = fopen("hashuri.in", "r");
FILE *fout = fopen("hashuri.out", "w");
int N, op, x;
for (fscanf(fin, "%d", &N); N; --N)
{
fscanf(fin, "%d %d", &op, &x);
if (op == 1) // inserare
{
insert(x);
continue;
}
if (op == 2) // stergere
{
del(x);
continue;
}
fprintf(fout, "%d\n", find(x));
}
return 0;
}