Cod sursa(job #2288225)

Utilizator DeleDelegeanu Alexandru Dele Data 22 noiembrie 2018 22:44:37
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.36 kb
#include <iostream>
#include <fstream>
#include <vector>
std::ifstream f("hashuri.in");
std::ofstream g("hashuri.out");
const int capacity = 499999;
class HashMap {
private:
	std::vector<int> table[capacity];
	int getHashKey(int value) {
		return value % capacity;
	}
	std::vector<int>::iterator findValue(int key, int value) {
		std::vector<int>::iterator sf = table[key].end();
		std::vector<int>::iterator it;
		for (it = table[key].begin(); it != sf; ++it)
			if (*it == value)return it;
		return sf;
	}
public:
	HashMap() {

	}
	void insert(int value) {
		int key = this->getHashKey(value);
		std::vector<int>::iterator it = findValue(key, value);
		if (it == table[key].end())
			table[key].push_back(value);
	}

	bool search(int value) {
		int key = this->getHashKey(value);
		std::vector<int>::iterator it = findValue(key, value);
		return !(it == table[key].end());
	}

	void remove(int value) {
		int key = this->getHashKey(value);
		std::vector<int>::iterator it = findValue(key, value);
		if (it != table[key].end())
			table[key].erase(it);
	}

};
int main() {
	int n;
	f >> n;

	HashMap* h = new HashMap();

	int op, x;
	for (int i = 1; i <= n; ++i) {
		f >> op >> x;
		if (op == 1) {
			h->insert(x);
			continue;
		}
		if (op == 2) {
			h->remove(x);
			continue;
		}
		g << h->search(x) << '\n';
	}

	return 0;
}