Cod sursa(job #1721539)

Utilizator elena.marinicaMarinica Elena-Georgiana elena.marinica Data 25 iunie 2016 20:28:29
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.09 kb
#include <vector>
#include <cstdio>
#include <iostream>

#define NMAX 100000

std::vector<std::vector<int> > hash(NMAX);

int hash_func(int elem) {
	
	return elem % NMAX;
}

bool contains (int elem, int& elem_pos) {
	
	int pos = hash_func(elem);
	
	bool found = false;
	
	for (unsigned int i = 0; i < hash[pos].size(); i++) {
		if (elem == hash[pos][i]) {
			found = true;
			elem_pos = i;
			
			break;
		}
	}
	return found;
}

void add (int elem) {

	int p;
	
	if (!contains(elem, p)) {
		int pos = hash_func(elem);
		hash[pos].push_back(elem);
	}
}

void remove (int elem) {

	int p;	
	
	if (contains(elem, p)) {
		int pos = hash_func(elem);
		hash[pos].erase(hash[pos].begin() + p);
	}
}

int main() {
	
	int N, op, elem;
	
	FILE *fin = fopen("hashuri.in", "r");
	FILE *fout = fopen("hashuri.out", "w");
	
	fscanf(fin, "%d", &N);
	
	for (int i = 0 ; i < N; i++) {
		fscanf(fin, "%d %d", &op, &elem);
		
		if (op == 1) {
			add(elem);
		}
		else if (op == 2) {
			remove(elem);
		}
		else if (op == 3) {
			int p;
			int res = contains(elem, p);
			fprintf(fout, "%d\n", res);
		}
	}
	
	fclose(fin);
	fclose(fout);
}