Cod sursa(job #739599)

Utilizator SimeneSimene Robert Simene Data 23 aprilie 2012 15:56:17
Problema Hashuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 2.98 kb
//hashing with open adressing using template and without global variables Simene Robert Gr.135 F.M.I.
#include <iostream>
#include<stdio.h>
#include<fstream>
using namespace std;

template <class H>
class hash
    {
        private:
            int r1,r2;//cele doua numere cere se vor genera random
            static const int nmax=1000;
            bool *clear;
            H *a;//vectorul in care se face hash-ul
            int n;

        public:
            hash(int x)//constructorul clasei H
            {
               unsigned int has(H , int );
               unsigned int has(char, int );
                n=x;
                a=new H[nmax];
                clear=new bool[nmax];
                srand(time(NULL));//functia pentru cele 2 numere radom
                r1=rand()%nmax;
                srand(time(NULL));
                r2=rand()%nmax;
            }
            ~hash()
            {
                delete[] a;
                delete[] clear;
            }

    template<class H>//pt int
        unsigned int hash<H>::has(H x, int i)
        {
            int rez=abs((x%nmax+r1*i+r2*i*i))%nmax;
            return rez;
        }
    template<class H>//pt double
        unsigned int hash<H>::has(H x, int i)
        {
            double k=0.618033;
            int rez=abs( (int((k*x - int(k*x))*nmax)) %nmax+r1*i+r2*i*i )%nmax;
            return rez;
        }

    template<>//pt stinguri
        unsigned int hash<char *>::has(char *str, int i)
        {
            unsigned int sum=0;
			int j;
			for (j=0;j+4<str.length();j+=4)
				sum+=str[j] + 128*str[j+1] + 128*128*str[j+2] + 128*128*128*str[j+3];
			while (j<=str.length())
			{
				sum+=str[j];
				++j;
			}
			int rez=(sum%nmax+r1*i+r2*i*i)%nmax;
			return rez;
        }
        void inserare (H x)
		{
			int k=0;
			while (v[has(x,k)]!=0 && k<=nmax)
				k++;
			if (k>=n+1)
			{
				cout<<"NU mai este loc!";
				return;
			}
			v[has(x,k)]=x;
			clear[has(x,k)]=0;
        }

		void stergere (H x)
		{
			int k=0;
			while (v[has(x,k)]!=x && (v[has(x,k)]!=0 || clear[has(x,k)]==1) && k<=nmax)
				k++;
			if (v[has(x,k)]==0) return;
			else if (k<=n)
			{
				v[has(x,k)]=0;
				clear[has(x,k)]=1;
			}
		}

		bool cautare (H x)
		{
			int k=0;
			while (v[has(x,k)]!=x && (v[has(x,k)]!=0 || clear[has(x,k)]==1) && k<=nmax)
				k++;
			if (k<=n && v[has(x,k)]==x) return 1;
			else return 0;
		}

		void afisare ()
		{
			printf("A= ");
			for (int i=0;i<nmax;++i)
                printf("%d",a[i]);
            printf("\n");
		}
};


int main()
{
	int n;
	scanf("%d",&n);
	hash<unsigned int> h(n);
	freopen("hashuri.in","r",stdin);
	freopen("hashuri.out","w",stdout);
	unsigned int op,nr1;
	for (register int i=1;i<=n;i++)
	{
		scanf("%d %d",&op,&nr1);
		if (op==1)
		{
		    if (!h.cautare(x))  h.inserare(x);
        }
		else if (op==2) h.stergere(x);
		else if (op==3) printf("%d\n",h.cautare(x));
	}
	return 0;
}