Cod sursa(job #1029073)

Utilizator ady.adrianADRIAN ANDREI ady.adrian Data 14 noiembrie 2013 23:08:41
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.32 kb
#include <fstream>
#include <list>
#include <vector>

using namespace std;

class Hash {
	private:
		static const int magic = 666031;
		//list<unsigned int> container[magic];
		vector< list<unsigned int> > container;
		int h (unsigned int value);

	public:
		Hash();
		bool query (unsigned int& value);
		bool insert (unsigned int value);
		void remove (unsigned int value);
};

Hash::Hash(){
	for (int i = 0; i < magic; i++) {
		container.push_back(list<unsigned int>());
	}
}

int Hash::h(unsigned int value) {
	return value % magic;
}

bool Hash::query(unsigned int& value) {
	int key = h(value);
	for(auto& candidate : container[key]) {
		if (candidate == value)
			return true;
	}
	return false;
}

bool Hash::insert(unsigned int value) {
	int key = h(value);
	if (this->query(value)) {
		return false;
	}
	else {
		container[key].push_back(value);
		return true;
	}
}

void Hash::remove(unsigned int value) {
	int key = h(value);
	container[key].remove(value);
}

int main() {
	ifstream fin("hashuri.in");
	ofstream fout("hashuri.out");
	int linii, opcode;
	unsigned int number;
	Hash hash;
	fin >> linii;
	while (linii--) {
		fin >> opcode >> number;
		switch(opcode) {
			case 1:
				hash.insert(number);
				break;
			case 2:
				hash.remove(number);
				break;
			case 3:
				fout << (hash.query(number) ? 1 : 0) << "\n";
		}
	}
	return 0;
}