Cod sursa(job #2781143)

Utilizator radu.z5Zamfirescu Radu Ioan radu.z5 Data 8 octombrie 2021 17:01:48
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.32 kb
#include <fstream>
#include <list>

using namespace std;

struct Hash {
	const size_t hashTableSize = 8e5;
	list<long> *array;

	Hash() : array(new list<long>[hashTableSize]) {}
	~Hash() {
		delete[] array;
	}

	size_t hashIdx(const long &value) {
		return value % hashTableSize;
	}

	void add(const long &value) {
		auto id = hashIdx(value);
		auto &linkedList = array[id];
		for (auto it = linkedList.begin(); it != linkedList.end(); it++)
			if (*it == value)
				return;
		linkedList.push_back(value);
	}

	void erase(const long &value) {
		auto id = hashIdx(value);
		auto &linkedList = array[id];
		linkedList.remove(value);
	}

	bool contains(const long &value) {
		auto id = hashIdx(value);
		auto &linkedList = array[id];
		for (auto it = linkedList.begin(); it != linkedList.end(); it++)
			if (*it == value)
				return true;
		return false;
	}
};

int main(void) {
	ifstream in("hashuri.in");
	ofstream out("hashuri.out");
	size_t n, i, code, x;
	in >> n;

	Hash container{};

	for (i = 0; i < n; i++) {
		in >> code >> x;
		switch (code) {
			case 1:
				container.add(x);
				break;
			case 2:
				container.erase(x);
				break;
			case 3:
				out << container.contains(x) << '\n';
				break;
			default:
				break;
		}
	}

	in.close();
	out.close();
	return 0;
}