Cod sursa(job #1596382)

Utilizator dimavascan94VascanDumitru dimavascan94 Data 10 februarie 2016 22:45:43
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.16 kb
#include <iostream>
#include <vector>
using namespace std;

#define PRIME 7919
int N, op, x, list, size, pos;
vector<vector<int>> theHash;
bool found;

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

	for (int i = 0; i<PRIME; ++i)
	{
		theHash.push_back(vector<int>());
	}

	cin >> N;
	while (N > 0)
	{
		scanf("%d%d", &op, &x);
		list = x % PRIME;
		size = theHash[list].size();
		found = false;
		pos = -1;
		switch (op)
		{
		case 1://insert value, if does not exist
			for (int i = 0; i < size && !found; ++i)
			{
				found = theHash[list][i] == x;
			}

			if (!found)
			{
				theHash[list].push_back(x);
			}
			break;
		case 2://delete value, if exists
			for (int i = 0; i < size; ++i)
			{
				found = theHash[list][i] == x;
				if (found)
				{
					pos = i;
					break;
				}
			}

			if (found)
			{
				theHash[list].erase(theHash[list].begin() + pos);
			}
			break;
		case 3://try to find the value, and print output
			for (int i = 0; i < size && !found; ++i)
			{
				found = theHash[list][i] == x;
			}

			printf("%d\n", found);
			break;
		}
		N--;
	}

}