Cod sursa(job #922538)

Utilizator mariacMaria Constantin mariac Data 22 martie 2013 13:08:28
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 4.45 kb
// Constantin Maria - grupa 134

#include<fstream>
#include <stdlib.h>
#include <string.h>
#include <time.h>

using namespace std;

class Hash_Func
{
    int MOD;
    int a,b,c;
    int size_h;
public:
    Hash_Func(int&);
    void Generate();
    int Hash(int&);
    int Hash(unsigned int&);
    int Hash(char&);
    int Hash(double&);
    int Hash(float&);
    int Hash(string&);
};
Hash_Func::Hash_Func(int &size)
{
    size_h = size;
    Generate();
}
void Hash_Func::Generate()
{
    int mo[]={999983,999979,899981,900007,800011};
    int dif;
    dif = size_h - 1000000;
    a = rand () % 10000000 + 1;
    b = rand () % 20008900 + 1;
    c = rand () % 100800 + 1;
    MOD = mo[rand () % 5] + dif;
}
int Hash_Func::Hash(int &x)

{
    int val;

    val = (a * x + b + c*c) % MOD;
    return val;
}
int Hash_Func::Hash(char &x)

{
    return (a * x + b - c*c) % MOD;
}
int Hash_Func::Hash(unsigned int &x)

{
    return (a * x + b - c*c) % MOD;
}
int Hash_Func::Hash(double &x)

{
    return ((int)(a * x) + b - c*c) % MOD;
}
int Hash_Func::Hash(float &x)

{
    return ((int)(a * x) + b - c*c) % MOD;
}
int Hash_Func::Hash(string &x)

{
    int i,val=0;
    for( i=0; i < x.size(); i++ )
    {
        val = ( (val*256) + x[i] ) % MOD;
    }
    return (a * val + b - c*c) % MOD;
}
template<class T>
class Cuckoo_Hash
{
    T *h1, *h2;
    int size_h;
    Hash_Func *F1,*F2;

public:
    bool operator+=(T);
    void operator-=(T);
    bool operator==(T);
    void Citeste();
    Cuckoo_Hash(unsigned int);
    ~Cuckoo_Hash();

};

template<class T>
Cuckoo_Hash<T>::Cuckoo_Hash(unsigned int s)
{
    size_h = s;
    h1 = new T[size_h];
    h2 = new T[size_h];
    F1 = new Hash_Func(size_h);
    F2 = new Hash_Func(size_h);
}

template<class T>
Cuckoo_Hash<T>::~Cuckoo_Hash()
{
   delete[] h1;
   delete[] h2;
   delete F1;
   delete F2;
}
template<class T>
bool Cuckoo_Hash<T>::operator+=(T x)
{
    int ok = 0, st = 1,v1,v2;
    T aux;

    v1 = F1->Hash(x);
    v2 = F2->Hash(x);

    while( ok > -30 )
    {
        if( st == 1 )
        {
            if(!h1 [v1] || h1 [v1] == x)
            {
                h1 [v1] = x;
                return true;
            }
            else
            {
                st = -1;
                if( ok < 0)
                {
                    aux = x;
                    x =  h1 [v1];
                    h1 [v1] = aux;
                    v1 = F1->Hash(x);
                    v2 = F2->Hash(x);
                }
            }
        }
        if( st == -1)
        {
            if(!h2 [v2] || h2 [v2] == x)
            {

                h2 [v2] = x;
                return true;
            }

            else
            {
                st = 1;
                if( ok < 0)
                {
                    aux = h2[v2];
                    h2[v2] = x;
                    x = aux;
                    v1 = F1->Hash(x);
                    v2 = F2->Hash(x);

                }
            }
        }
        ok--;
    }
    return false;
}
template<class T>
void Cuckoo_Hash<T>::operator-=(T x)
{
    int v1,v2;
    v1 = F1->Hash(x);
    v2 = F2->Hash(x);
    if(h1[v1] == x) h1[v1] = 0;

        else if(h2[v2] == x ) h2[v2] = 0;
}
template<class T>
bool Cuckoo_Hash<T>::operator==(T x)
{
     int v1 = F1->Hash(x);
     int v2 = F2->Hash(x);

     if(h1 [v1] == x) return true;

     if(h2 [v2] == x ) return true;

    return false;
}
template<class T>
void Cuckoo_Hash<T>::Citeste ()
{
    int n, caz, i;
    T x;

    int ok = 0;
    while (ok == 0)
    {
        ok = 1;
        ifstream fin("hashuri.in");
        ofstream fout("hashuri.out");
        fin>>n;
        memset(h1,0,size_h);
        memset(h2,0,size_h);
        F1->Generate();
        F2->Generate();
        for (i = 1; i <= n && ok; i++)
        {
            fin>> caz>> x;

            switch (caz)
            {
                case 1: if(!(*this+=x))
                        {
                            ok = 0;
                        }
                        break;
                case 2: *this-=x; break;
                case 3: fout <<(*this==x)<<"\n"; break;

            }
        }
        fin.close();
        fout.close();


    }
}




int main()
{


    srand (time(NULL));
    Cuckoo_Hash <unsigned int> H(1000000);
    H.Citeste ();
    return 0;

}