Pagini recente » Cod sursa (job #1577108) | Cod sursa (job #1877907) | Cod sursa (job #976097) | Cod sursa (job #2342433) | Cod sursa (job #1596382)
#include <iostream>
#include <vector>
using namespace std;
#define PRIME 7919
int N, op, x, list, size, pos;
vector<vector<int>> theHash;
bool found;
int main()
{
freopen("hashuri.in", "r", stdin);
freopen("hashuri.out", "w", stdout);
for (int i = 0; i<PRIME; ++i)
{
theHash.push_back(vector<int>());
}
cin >> N;
while (N > 0)
{
scanf("%d%d", &op, &x);
list = x % PRIME;
size = theHash[list].size();
found = false;
pos = -1;
switch (op)
{
case 1://insert value, if does not exist
for (int i = 0; i < size && !found; ++i)
{
found = theHash[list][i] == x;
}
if (!found)
{
theHash[list].push_back(x);
}
break;
case 2://delete value, if exists
for (int i = 0; i < size; ++i)
{
found = theHash[list][i] == x;
if (found)
{
pos = i;
break;
}
}
if (found)
{
theHash[list].erase(theHash[list].begin() + pos);
}
break;
case 3://try to find the value, and print output
for (int i = 0; i < size && !found; ++i)
{
found = theHash[list][i] == x;
}
printf("%d\n", found);
break;
}
N--;
}
}