Cod sursa(job #3129718)

Utilizator livliviLivia Magureanu livlivi Data 15 mai 2023 17:07:52
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.15 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

template<const int P>
class HashTable {
	vector<int> content;
	vector<int> next;
	vector<int> start;

public:
	HashTable() {
		start.resize(P);
		next.resize(1);
		content.resize(1);
	}

	void insert(int x) {
		int hash = x % P;

		content.push_back(x);
		next.push_back(start[hash]);
		start[hash] = content.size() - 1;
	}

	bool find(int x) {
		int hash = x % P;

		for (int i = start[hash]; i != 0; i = next[i]) {
			if (content[i] == x) {
				return true;
			}
		}

		return false;
	}

	void erase(int x) {
		int hash = x % P;

		int i = start[hash];
		if (content[i] == x) {
			start[hash] = next[i];
		} else {
			while (next[i] != 0 && content[next[i]] != x) {
				i = next[i];
			}
			if (content[next[i]] == x) {
				next[i] = next[next[i]];
			}
		}
	}
};

int main() {
	ifstream cin("hashuri.in");
	ofstream cout("hashuri.out");

	HashTable<666013> set;

	int n; cin >> n;
	for (int i = 0; i < n; i++) {
		int op, x; cin >> op >> x;

		if (op == 1) {
			set.insert(x);
		} else if (op == 2) {
			set.erase(x);
		} else {
			cout << set.find(x) << "\n";
		}
	}

	return 0;
}