Cod sursa(job #609624)

Utilizator mika17Mihai Alex Ionescu mika17 Data 22 august 2011 15:11:12
Problema Hashuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.32 kb
#include <fstream>
#include <list>
#include <cmath>

#define uint32 unsigned int
#define uint64 unsigned long long

class HashTable
{
	private: const static uint32 p = 16;
	private: const static uint32 HASHTABLE_SIZE = 1 << p;
	private: std::list<uint32> hash[HASHTABLE_SIZE];

	private: const static uint32 w = sizeof(uint32) * 8;
	private: const static uint32 a = floor((sqrt(5.0) - 1)/2 * ( 1 << (w - 1) ));

	private: uint32 f(const uint32& x)
	{
		return (uint64)x * a >> (2*w - p);
	}
	public: void insert(const uint32& x)
	{
		if(!search(x))
			hash[f(x)].push_back(x);
	}
	public: void remove(const uint32& x)
	{
		if(search(x))
			hash[f(x)].remove(x);
	}
	public: bool search(const uint32& x)
	{
		for(std::list<uint32>::iterator I = hash[f(x)].begin(); I != hash[f(x)].end(); ++I)
		{
			if(*I == x)
				return true;
		}
		return false;
	}
};

int main()
{
	std::ifstream f("hashuri.in");
	std::ofstream g("hashuri.out");

	uint32 ops; f >> ops;
	
	HashTable * p = new HashTable;

	while(ops--)
	{
		char type; uint32 num; f >> type >> num;
		switch(type)
		{
			case '1':
				p->insert(num);
				break;
			case '2':
				p->remove(num);
				break;
			case '3':
				g << ( p->search(num) ? 1 : 0 ) << '\n';
				break;
			default:
				break;
		}
	}
	
	delete p;
	return 0;
}