Cod sursa(job #1541780)

Utilizator andrei_diaconuAndrei Diaconu andrei_diaconu Data 4 decembrie 2015 15:53:53
Problema Hashuri Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 1.28 kb
#include <fstream>
#include <vector>

#define MOD 666013

using namespace std;

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

int queries;
vector<int> hashTable[MOD + 10];

int hashFunction(int val)
{
	int h = 0, g;
	while (val != 0) {
		h = (h << 4) + val % 10;
		val /= 10;

		if (g = h & 0xF0000000)
			h ^= g >> 24;

		h &= ~g;
	}
	return h;
}

vector<int>::iterator hashFind(int val)
{
	int key = hashFunction(val);

	for (vector<int>::iterator it = hashTable[key].begin(); it != hashTable[key].end(); it++)
		if (*it == val)
			return it;

	return hashTable[key].end();
}

void insertHash(int val)
{
	int key = hashFunction(val);

	if (hashFind(val) == hashTable[key].end())
		hashTable[key].push_back(val);
}

void deleteHash(int val)
{
	int key = hashFunction(val);

	vector<int>::iterator it = hashFind(val);

	if (hashFind(val) != hashTable[key].end())
		hashTable[key].erase(it);
}

int main()
{
	f >> queries;

	int op, elem;
	for (int i = 1; i <= queries; i++) {
		f >> op >> elem;

		if (op == 1) {
			insertHash(elem);
		}
		else if (op == 2) {
			deleteHash(elem);
		}
		else {
			int key = hashFunction(elem);

			if (hashFind(elem) != hashTable[key].end())
				g << "1\n";
			else
				g << "0\n";
		}
	}
}