Cod sursa(job #1596355)

Utilizator dimavascan94VascanDumitru dimavascan94 Data 10 februarie 2016 22:23:04
Problema Hashuri Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 1.17 kb
#include <iostream>
#include <vector>
using namespace std;

#define PRIME 2
int N, op, x;
vector<vector<int>> theHash;

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);
		int list = x % PRIME;
		int size = theHash[list].size();
		bool found = false;
		int 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--;
	}

}