Cod sursa(job #2612076)

Utilizator DawlauAndrei Blahovici Dawlau Data 8 mai 2020 14:29:31
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.37 kb
#include <fstream>
#include <vector>
#include <algorithm>
#include <typeinfo>

using namespace std;

ifstream fin("hashuri.in");
ofstream fout("hashuri.out");

class hashmap {

	const int mod;
	vector < vector <int> > hash;

	vector <int> :: iterator FindPointer(const int &);

public:

	hashmap(const int &);
	void Insert(const int &);
	bool Find(const int &);
	void Erase(const int &);
};
 // pick a prime so you minimize the collisions
hashmap::hashmap(const int &mod = 666013) : mod(mod) { hash.resize(mod); }

void hashmap::Insert(const int &no) {

	if (Find(no)) return;
	hash[no % mod].push_back(no);
}

void hashmap::Erase(const int &no) {

	vector <int> ::iterator found = FindPointer(no);
	if (found == hash[no % mod].end()) return;
	hash[no % mod].erase(found);
}

bool hashmap::Find(const int &no) { return (FindPointer(no) != hash[no % mod].end()); }

vector <int> :: iterator hashmap::FindPointer(const int &no) {
	
	int key = no % mod;
	for (int idx = 0; idx < (int)hash[key].size(); ++idx)
		if (hash[key][idx] == no)
			return hash[key].begin() + idx;
	return hash[key].end();
}

int main() {

	hashmap HashMap;

	int n;
	fin >> n;

	for (; n; --n) {

		int code, no;
		fin >> code >> no;

		if (code == 1)
			HashMap.Insert(no);
		else if (code == 2)
			HashMap.Erase(no);
		else fout << HashMap.Find(no) << '\n';
	}
}