Cod sursa(job #750408)

Utilizator blk.irineluIrina Ursateanu blk.irinelu Data 22 mai 2012 00:16:12
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.41 kb
// Double hashing
// Ursateanu Irina Mihaela, Grupa 135
#include <fstream>
#include <stdio.h>
#define mod1 3000000
#define mod2 500021
#define mod3 500007

using namespace std;

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

class hash
{
	private:
		int *H; // Folosim un vector pentru hash
		int size_of_hash;
	public:
		hash(int size)
		 {
			H=new int[size];
			size_of_hash=size;
          }

		int hash_table(int X,int i);
		void insert(int X);
		int find(int X);
		void erase(int X);
};

int hash::hash_table(int X,int i)
{
    return ((X%mod2)+i*(1+X%mod3))%mod1;
}

int hash::find(int X)
{
	int i,aux;// aux retine pozitia in vectorul de hash
	for(i=0;i<mod1;i++)
	 {
		aux=hash_table(X,i);
		if(H[aux]==0)
			return -1;
		if(H[aux]==X)
			return aux;
	}
	return -1;
}

void hash::insert(int X)
{
	int i, aux;// aux retine pozitia
	if(find(X)==-1)
		for(i=0;i<mod1;i++)
        {
			aux=hash_table(X,i);
			if(H[aux]<=0)
            {
				H[aux]=X;
				return; // Paraseste cand gaseste o pozitie libera
			}
		}
}

void hash::erase(int X)
{
	int aux;
	aux=find(X);
	if(aux!=-1)
		H[aux]=-1;
}

int main()
{

	hash object(mod1);
	int i,op,x,n;
	f>>n;
	for(i=1;i<=n;i++)
    {
		f>>op>>x;
		if(op==1)
			object.insert(x);
		if(op==2)
			object.erase(x);
		if(op==3)
			if(object.find(x)==-1)g<<"0\n";
			else g<<"1\n";
	}
	return 0;
}