Cod sursa(job #679290)

Utilizator cpblncCristian Grajdeanu cpblnc Data 12 februarie 2012 23:56:17
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.22 kb
#include <fstream>
#include <iostream>
#include <vector>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

int size = 10000;

int getHash(int i){
	return i%size;
}

int find(int el, vector<int>& v){
	for(int k = 0; k < v.size(); k++) {
		if(v[k] == el){
			return k;
		} 
	}
	return -1;
}

int main(int argc, char** argv){

	vector< vector<int> > hashTable;
	for(int i = 0; i < size; i++){
		vector<int> v;
		hashTable.push_back(v);
	}
	int count, op, j;
	
	ifstream in;
	in.open("hashuri.in");
	in >> count;
	ofstream out;
	out.open("hashuri.out");
	
	for(int i = count; i; i--){
		in >> op >> j;
		int hash, pos;
		switch(op){
			case 1:
				hash = getHash(j);
				pos = find(j, hashTable[hash]);
				if(pos == -1){
					hashTable[hash].push_back(j);
				}
				break;		
			case 2:
				hash = getHash(j);
				pos = find(j, hashTable[hash]);
				if(pos != -1){
					hashTable[hash].erase(hashTable[hash].begin() + pos);
				}
				break;
			case 3:
				hash = getHash(j);
				pos = find(j, hashTable[hash]);
				if(pos == -1){
					out << 0 << "\n";
				} else {
					out << 1 << "\n";
				}
				break;
			default:
				exit(-1);
		}
	}

	in.close();
	out.close();
}