Cod sursa(job #963231)

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

#define N 44497

using namespace std;
/*
template<typename Tkey> struct nod {
	Tkey key;
};*/
     
template<typename Tkey> class Hashtable {
	private:
		vector<unsigned int> H[N];
		//unsigned int HMAX;
	public:
		/*Hashtable(unsigned int h)
		{
			HMAX = h;
			H = new ForwardList< struct nod<Tkey> >[HMAX];
		}

		~Hashtable()
		{
			delete[] H;
		}*/
		
	void put(Tkey key)
	{
		if(!hasKey(key))
		{
			unsigned int poz;
			poz = key % N;
			//nod<Tkey> x;
			//x.key = key;
			H[poz].push_back(key);
			
		}
	}
	
	bool hasKey(Tkey key)
	{
		unsigned int poz;
		poz = key % N;
		/*struct elem<struct nod<Tkey> >* paux;
		paux = H[poz].pfirst;
		while(paux && paux->info.key != key)
			paux = paux->next;
		return paux != NULL;*/
		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;
		/*elem<nod<Tkey> >* paux = H[poz].pfirst;
		while(paux && paux->info.key != key)
			paux = paux->next;
		H[poz].remove(paux);*/
		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));
	}
	
}