Cod sursa(job #766321)

Utilizator d.andreiDiaconeasa Andrei d.andrei Data 10 iulie 2012 23:50:40
Problema Hashuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.14 kb
#include <iostream>
#include <fstream>

using namespace std;

#define mod 666013

ifstream f("hashuri.in");
ofstream g("hashuri.out");

class Hash{
	
	int *H;
	int size;
	
public:
	
	Hash(int N){
		
		H=new int[N];
		size=N;
	}
	~Hash(){
		
		delete[] H;
	}
	int h(int x, int i){
	return (x%mod+i*(1+x%(mod-1)))%mod;
	}
	void insert(int X);
	int search(int X);
	void erase(int X);
	
};

int Hash::search(int X){
	
	for (int i=0;i<mod;++i){
		int f=this->h(X,i);
	    if (this->H[f]==0)
			return -1;
	    if (this->H[f]==0)
			 return f;
	}
		 
}

void Hash::insert(int X){
	
	if (search(X)==-1)
	for (int i=0;i<mod;++i){
		int f=this->h(X,i);
		if (this->H[f]<=0){
	    this->H[f]=X;
		return ;
		}
    }
			
}

	

void Hash::erase(int X){
	
	int f=this->search(X);
	if (f!=-1)
	this->H[f]=-1;
}


int main(){
	
	Hash Q(mod);
	
	int T,tip,X;
	
	f>>T;
	
	while(T--){
	
		f>>tip>>X;
		
		if (tip==1){
				Q.insert(X);
		}
		else
		if (tip==2){
				Q.erase(X);
		}
		else{
			int w=Q.search(X);
			if (w!=-1)
				g<<"1\n";
			else
				g<<"0\n";
		}
	}
	
	return 0;
}