Mai intai trebuie sa te autentifici.

Cod sursa(job #2531490)

Utilizator radustn92Radu Stancu radustn92 Data 26 ianuarie 2020 12:43:12
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.24 kb
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;

const int MOD = 1e6+3;
const int SENTINEL = -1;
vector<int> buckets[MOD];

int ops;

inline int hashFunction(int key) {
	return key % MOD;
}

void insertHashTable(int bucket, int value) {
	bool found = false;
	for (auto elem : buckets[bucket]) {
		if (elem == value) {
			found = true;
			break;
		}
	}

	if (!found) {
		buckets[bucket].push_back(value);
	}
}

void deleteHashTable(int bucket, int value) {
	for (size_t i = 0; i < buckets[bucket].size(); i++) {
		if (buckets[bucket][i] == value) {
			buckets[bucket][i] = SENTINEL;
			break;
		}
	}
}

bool searchHashTable(int bucket, int value) {
	for (auto elem : buckets[bucket]) {
		if (elem == value) {
			return true;
		}
	}

	return false;
}

int main() {
	freopen("hashuri.in", "r", stdin);
	freopen("hashuri.out", "w", stdout);

	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	cin >> ops;
	int opType, x;
	for (int op = 0; op < ops; op++) {
		cin >> opType >> x;
		switch (opType) {
			case 1: {
				insertHashTable(hashFunction(x), x);
				break;
			}
			case 2: {
				deleteHashTable(hashFunction(x), x);
				break;
			}
			case 3: {
				printf("%d\n", searchHashTable(hashFunction(x), x));
				break;
			}
		}
	}

	return 0;
}