Cod sursa(job #943382)

Utilizator fhandreiAndrei Hareza fhandrei Data 25 aprilie 2013 10:20:26
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.84 kb
// Include
#include <fstream>
#include <cstring>
#include <cstdlib>
using namespace std;

// Constante
const int modulo = 450001;

// Clase
class typeHash
{
	int *pHash[modulo];
	int pSize[modulo];
	int pTotalSize;
	
	public:
	
	typeHash();
	void insert(int val);
	bool erase(int val);
	int* find(int val);
	int size();
	bool empty();
	void clear();
};

// Variabile
ifstream in("hashuri.in");
ofstream out("hashuri.out");

int num;
int type, val;
typeHash hash;

// Main
int main()
{
	in >> num;
	while(num--)
	{
		in >> type >> val;
		if(type == 1)
		{
			if(!hash.find(val))
				hash.insert(val);
		}
		else if(type == 2)
			hash.erase(val);
		else
			out << (hash.find(val)? 1 : 0) << '\n';
	}
	
	in.close();
	out.close();
	return 0;
}

typeHash::typeHash()
{
	for(int i=0 ; i<modulo ; ++i)
		pHash[i] = (int*) malloc(sizeof(int));
	
	memset(pSize, 0, sizeof(pSize));
}

void typeHash::insert(int val)
{
	int line = val % modulo;
	
	pHash[line][pSize[line]++] = val;
	int *temp = (int*)realloc(pHash[line], (pSize[line]+1) * sizeof(int));
	pHash[line] = temp;
	
	++pTotalSize;
}

bool typeHash::erase(int val)
{
	int line = val % modulo;
	
	for(int i=0 ; i<pSize[line] ; ++i)
	{
		if(pHash[line][i] == val)
		{
			swap(pHash[line][i], pHash[line][--pSize[line]]);
			
			int *temp = (int*)realloc(pHash[line], (pSize[line]+1) * sizeof(int));
			pHash[line] = temp;
			
			--pTotalSize;
			return true;
		}
	}
	
	return false;
}

int* typeHash::find(int val)
{
	int line = val % modulo;
	
	for(int i=0 ; i<pSize[line] ; ++i)
	{
		if(pHash[line][i] == val)
			return &pHash[line][i];
	}
	
	return '\0';
}

int typeHash::size()
{
	return pTotalSize;
}

bool typeHash::empty()
{
	return pTotalSize? true : false;
}

void typeHash::clear()
{
	typeHash();
}