Pagini recente » Istoria paginii utilizator/pascuflorinfmiuvt | Cod sursa (job #1728097) | Cod sursa (job #1221695) | Cod sursa (job #344028) | Cod sursa (job #1596355)
#include <iostream>
#include <vector>
using namespace std;
#define PRIME 2
int N, op, x;
vector<vector<int>> theHash;
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);
int list = x % PRIME;
int size = theHash[list].size();
bool found = false;
int 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--;
}
}