Cod sursa(job #2246385)

Utilizator test_accNo Name test_acc Data 27 septembrie 2018 01:11:30
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.32 kb
#include <fstream>
#include <vector>

using namespace std;

ifstream f("hashuri.in");
ofstream g("hashuri.out");

class Hash {
private:
	static const int HASHSIZE = 666013;
	vector<int> hash[HASHSIZE];

	inline int hash_func(int key) {
		return key % HASHSIZE;
	}
	vector<int>::iterator find_it(int bucket, int searched_key) {
		for (auto it = hash[bucket].begin(); it != hash[bucket].end(); ++it)
			if (searched_key == *it)
				return it;
		return hash[bucket].end();
	}

public:
	Hash();
	void ins(int key);
	void del(int key);
	int find(int key);
};

Hash::Hash() {

}

void Hash::ins(int key) {
	int bucket = hash_func(key);
	if (find_it(bucket, key) == hash[bucket].end())
		hash[bucket].push_back(key);
}

void Hash::del(int key) {
	int bucket = hash_func(key);
	auto it = find_it(bucket, key);
	if (it != hash[bucket].end())
		hash[bucket].erase(it);
}

int Hash::find(int key) {
	int bucket = hash_func(key);
	if (find_it(bucket, key) != hash[bucket].end())
		return 1;
	return 0;
}

int main() {
	int inps;
	f >> inps;

	int op, key;
	Hash *hash = new Hash();
	for (int i = 1; i <= inps; i++) {
		f >> op >> key;
		switch (op) {
		case 1:
			hash->ins(key);
			break;
		case 2:
			hash->del(key);
			break;
		case 3:
			g << hash->find(key) << "\n";
			break;
		}
	}
}