Pagini recente » Cod sursa (job #1805689) | Cod sursa (job #1157869) | Cod sursa (job #1164629) | Cod sursa (job #2334346) | Cod sursa (job #1573537)
#include <fstream>
#include <vector>
using namespace std;
const int PRIME = 666013;
const int HASH_SIZE = PRIME;
int hashFunction(int x)
{
return x % PRIME;
}
bool hashFind(int x, vector<int> h[])
{
bool found = false;
int pos = hashFunction(x);
for (int i = 0; i < h[pos].size() && !found; i++)
if (h[pos][i] == x)
found = true;
return found;
}
void hashInsert(int x, vector<int> h[])
{
if (!hashFind(x, h))
h[hashFunction(x)].push_back(x);
}
void hashRemove(int x, vector<int> h[])
{
int pos = hashFunction(x);
for (int i = 0; i < h[pos].size(); i++)
if (h[pos][i] == x)
{
h[pos][i] = h[pos].back();
h[pos].pop_back();
}
}
int main()
{
vector<int> h[HASH_SIZE];
int N, op, x, i;
ifstream f("hashuri.in");
ofstream g("hashuri.out");
f >> N;
for (i = 0; i < N; i++)
{
f >> op >> x;
if (op == 1)
hashInsert(x, h);
else if (op == 2)
hashRemove(x, h);
else if (op == 3)
g << hashFind(x, h) << '\n';
}
f.close();
g.close();
return 0;
}