Cod sursa(job #239823)

Utilizator gabitzish1Gabriel Bitis gabitzish1 Data 5 ianuarie 2009 22:27:16
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.01 kb
#include <cstdio>
#include <vector>
using namespace std;

#define MOD 666013

int N;
vector <int> h[MOD];

int hash(int x)
{
	return x % MOD;
}

void swap(int &x, int &y)
{
	int aux = x; x = y; y = aux;
}

void adaug(int x)
{
	h[hash(x)].push_back(x);
}

void sterg(int x)
{
	int n, poz = hash(x), i;
	n = h[poz].size();
	for (i = 0; i < n; i++) 
		if (h[poz][i] == x) 
		{
			swap(h[poz][i], h[poz][n - 1]);
			h[poz][n - 1] = -1;
			break;
		}
}

int find(int x)
{
	int i, n, poz = hash(x);
	n = h[poz].size();
	for (i = 0; i < n; i++) if (h[poz][i] == x) return 1;
	return 0;
}


int main()
{
	freopen("hashuri.in","r",stdin);
	freopen("hashuri.out","w",stdout);

	int i, x, op;
	scanf("%d", &N);

	for (i = 1; i <= N; i++)
	{
		scanf("%d %d", &op, &x);
		switch(op)
		{
		case 1:
			{
				adaug(x);
				break;
			}
		case 2:
			{
				sterg(x);
				break;
			}
		case 3:
			{
				printf("%d\n", find(x));
				break;
			}
		}
	}
	return 0;
}