Cod sursa(job #921643)

Utilizator chimistuFMI Stirb Andrei chimistu Data 21 martie 2013 10:25:53
Problema Hashuri Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 3.58 kb
#include <iostream>
#include <fstream>
#include <ctime>
#include <cstdlib>
#include <cstring>
#define size 1000001

using namespace std;

class hash_function
{
    int a,b,c,d,k;
public:
    hash_function();
    int functie(int);
    int functie(float);
    int functie(double);
    int functie(string);
};

hash_function::hash_function()
{
    k = 666013;
    a = rand() % size;
    b = rand() % size;
    c = (rand() % size + 15) % k;
    d = (rand() % size + 24) % k;

}

int hash_function::functie(int x)
{
    return ((a % k + b % k + (x)%k) % size);
}

int hash_function::functie(float x)
{
    float q = 0.567;
    return ((int)((a % k + b % k) * q +  (int)x % k) % size);
}

int hash_function::functie(double x)
{
    float q = 0.645;
    return ((int)((a % k + b % k) * q + (int)x % k) % size);
}

int hash_function::functie(string x)
{
    int q;
    q = x.length();
    return (((a % k + b % k) + q % k) % size);
}

template <class T>
class cuckoo_hash
{
    T *hash1, *hash2;
    hash_function func1,func2;
public:
    cuckoo_hash<T>();
    ~cuckoo_hash<T>();
    bool cauta(T);
    bool insereaza(T);
    void sterge(T);
    void make_hash();
};

template <class T>
cuckoo_hash<T>::cuckoo_hash()
{
    hash1 = new int[size];
    hash2 = new int[size];
    for (int i=0;i<size;i++)
    {
        hash1[i]=0;
        hash2[i]=0;
    }
}

template <class T>
cuckoo_hash<T>::~cuckoo_hash()
{
    delete[] hash1;
    delete[] hash2;
}

template <class T>
bool cuckoo_hash<T>::cauta(T x)
{
    int f1 = func1.functie(x);
    int f2 = func2.functie(x);
    if (hash1[f1] == x || hash2[f2] == x)
        return true;
    else
        return false;
}

template <class T>
void cuckoo_hash<T>::sterge(T x)
{
    int f1 = func1.functie(x);
    int f2 = func2.functie(x);
    if (hash1[f1] == x)
        hash1[f1] = 0;
    else
        if (hash2[f2] == x)
            hash2[f2] = 0;
}

template <class T>
bool cuckoo_hash<T>::insereaza(T x)
{
    if (cauta(x))
        return true;
    else
    {
        int f1 = func1.functie(x);
        int f2 = func2.functie(x);
        int i=0;
        int aux;
        while (i<50)
        {
            if (hash1[f1] == 0)
            {
                hash1[f1] = x;
                i=100;
                return true;
            }
            aux = hash1[f1];
            hash1[f1] = x;
            x = aux;
            if (hash2[f2] == 0)
            {
                hash2[f2] = x;
                i=100;
                return true;
            }
            aux = hash2[f2];
            hash2[f2] = x;
            x = aux;
            i++;
        }
        return false;
    }
}

template <class T>
void cuckoo_hash<T>::make_hash()
{

    bool da = false;
    while (!da)
    {
        ifstream f("hashuri.in");
        ofstream g("hashuri.out");
        int n,op,val;
        f >> n;
        for (int i=0;i<n;i++)
        {
            f >> op >> val;
            if (op==1)
            {
                insereaza(val);
               /* if (!insereaza(val))
                {
                    da = false;
                    break;
                }*/
            }
            if (op == 2)
            {
                sterge(val);
            }
            if (op == 3)
            {
                g << (int)cauta(val) << "\n";
            }
        }
        da = true;
        f.close();
        g.close();
    }
}


int main()
{
    srand(time(NULL));
    cuckoo_hash<int> q;
    q.make_hash();
    return 0;
}