Cod sursa(job #1450614)

Utilizator ramhackNastase Ramon ramhack Data 13 iunie 2015 22:10:46
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.66 kb
#include <iostream>
#include <list>
#include <cstdio>
#define REF 666013
using namespace std;

class HashTable {
	
private:
	list<int> *hash_list;
public:

	HashTable() {
		hash_list = new list<int>[REF + 2];
	}
	~HashTable() {}

	int hash(int value) {
		return value % REF;
	}
	void put(int value) {

		int hash_value = hash(value);

		if(searchKey(value) == REF) {
			hash_list[hash_value].push_back(value);
		}
	}
	bool remove(int value) {

		int hash_value = hash(value);

		if(get(value)) {
			hash_list[hash_value].erase(getIteratorKey(value)); 			
			return true;			
		}
		return false;
	}
	int searchKey(int value) {

		int hash_value = hash(value);
		list<int>::iterator it;

		for(it = hash_list[hash_value].begin(); it != hash_list[hash_value].end(); ++it) {

			if(*it == value) {
				return *it;
			}
		}
		return REF;
	}


	list<int>::iterator getIteratorKey(int value) {

		int hash_value = hash(value);
		list<int>::iterator it;

		for(it = hash_list[hash_value].begin(); it != hash_list[hash_value].end(); ++it) {

			if(*it == value) {
				return it;
			}
		}
		return hash_list[hash_value].end();
	}

	int get(int value) {

		if(searchKey(value) == REF) {
			return 0;
		}
		return 1;
	}

};

int main(int argc, char const *argv[])
{
	
	FILE *f, *fOut;
	fOut = fopen("hashuri.out", "w");
	if((f = fopen("hashuri.in", "r")) == NULL) {
		fprintf(stderr, "Can't open file\n");
		return 0;
	}

	int N, op, x;
	HashTable h;
	fscanf(f, "%d", &N);

	while(N > 0) {

		fscanf(f, "%d %d", &op, &x);

		if(op == 1) {
			h.put(x);
		}
		else if(op == 2) {
			h.remove(x);
		}
		else if(op == 3) {
			fprintf(fOut, "%d\n", h.get(x));
		}

		N--;
	}

	fclose(f);
	fclose(fOut);
	return 0;
}