Cod sursa(job #766319)

Utilizator d.andreiDiaconeasa Andrei d.andrei Data 10 iulie 2012 23:40:40
Problema Hashuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.97 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);
	
};

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

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

void Hash::erase(int X){
	
	int f=this->h(X,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
			g<<Q.search(X)<<"\n";
	}
	
	return 0;
}