Cod sursa(job #745136)

Utilizator blk.irineluIrina Ursateanu blk.irinelu Data 10 mai 2012 16:28:30
Problema Hashuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.37 kb
#include <fstream>
#include <stdio.h>
#define mod1 666013
#define mod2 100021
#define mod3 100007

using namespace std;

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

class hash
{
	private:
		int H[mod1+10];
		int size_of_hash;
	public:
		hash(int size)
		 {
			size_of_hash=size;
          }

		void insert(int X);
		int find(int X);
		void erase(int X);
		int hash1(int X);
		int hash2(int X);
		int final_hash(int X, int i);
};

int hash::hash1(int X)
{
	return X%mod2;
}

int hash::hash2(int X)
{
	return 1+(X%mod3);
}

int hash::final_hash(int X, int i)
{
	return (hash1(X)+i*hash2(X))%mod1;
}

int hash::find(int X)
{
	int i,aux;
	for(i=0;i<mod1;i++)
	 {
		aux=((X%mod2)+i*(1+X%mod3))%mod1;
		if(H[aux]==0)
			return -1;
		if(H[aux]==X)
			return aux;
	}
	return -1;
}

void hash::insert( int X )
{
	int i, aux;
	if(find(X)==-1)
		for(i=0;i<mod1;i++)
        {
			aux=((X%mod2)+i*(1+X%mod3))%mod1;
			if(H[aux]<=0)
            {
				H[aux] = X;
				return;
			}
		}
}

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";
			else g<<"1";
	}
	return 0;
}