Cod sursa(job #963247)

Utilizator Cezar_16Cezar Ghimbas Cezar_16 Data 16 iunie 2013 21:33:43
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.14 kb
#include<stdlib.h>
#include<stdio.h>
#include<iostream>
#include<vector>

#define N 44497

using namespace std;
     
template<typename Tkey> class Hashtable {
	private:
		vector<unsigned int> H[N];
	public:	
		void put(Tkey key)
		{
			if(!hasKey(key))
			{
				unsigned int poz;
				poz = key % N;
				H[poz].push_back(key);
			
			}
		}
	
		bool hasKey(Tkey key)
		{
			unsigned int poz;
			poz = key % N;
			vector<unsigned int>::iterator it;
			for(it = H[poz].begin(); it != H[poz].end(); ++it)
				if(*it == key)
					return true;
			return false;
		}
	
		void remove(Tkey key)
		{
			unsigned int poz = key % N;
			vector<unsigned int>::iterator it;
			for(it = H[poz].begin(); it != H[poz].end(); ++it)
				if(*it == key)
				{
					H[poz].erase(it);
					return;
				}
		}
};
int main()
{
	unsigned int n;
	short int op;
	unsigned int x;
	freopen("hashuri.in", "r", stdin);
	freopen("hashuri.out", "w", stdout);
	scanf("%d", &n);
	
	Hashtable<unsigned int> hash;
	for(unsigned int i = 0; i < n; i++)
	{
		scanf("%hd", &op);
		scanf("%d", &x);
		if(op == 1)
			hash.put(x);
		else if(op == 2)
			hash.remove(x);
		else
			printf("%d\n", hash.hasKey(x));
	}
	
}