Pagini recente » Cod sursa (job #3138069) | Cod sursa (job #1432049) | Cod sursa (job #890459) | Cod sursa (job #1734117) | Cod sursa (job #1573534)
#include <fstream>
#include <vector>
using namespace std;
const int PRIME = 4999999;
const int HASH_SIZE = PRIME;
vector<int> h[HASH_SIZE];
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()
{
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;
}