Cod sursa(job #907060)

Utilizator maritimCristian Lambru maritim Data 7 martie 2013 16:34:37
Problema Hashuri Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 4.36 kb
#include<iostream>
#include<vector>
#include<stdlib.h>
#include<time.h>
#include<fstream>
#include<ctype.h>
using namespace std;

class CuckooHash
{
    private:
        int hash1,hash2,M1,M2,size;
        int uninserted;
        int  *Hash1,*Hash2;

        void makeSizes(void);
        int hashFunction1(int x);
        int hashFunction2(int x);
        void remakeFunctions(void);
        int reinsert(int x);
        int rehash(void);
        int insertTry(int x);

    public:
        CuckooHash(void);
        CuckooHash(int M1,int M2);
        int insert(int x);
        int remove(int x);
        int find(int x);
};

CuckooHash::CuckooHash(void)
{
    srand(time(NULL));

    makeSizes();

    Hash1 = new int[3000000];
    Hash2 = new int[3000000];

    remakeFunctions();
}

CuckooHash::CuckooHash(int M1,int M2)
{
    this->M1 = M1;
    this->M2 = M2;

    Hash1 = new int[3000000];
    Hash2 = new int[3000000];

    remakeFunctions();
}

int CuckooHash::hashFunction1(int x)
{
    return (1LL*hash1*(x%M1)+hash2)%M1;
}

int CuckooHash::hashFunction2(int x)
{
    return (1LL*hash2*(x%M2)+hash1)%M2;
}

void CuckooHash::remakeFunctions(void)
{
    hash1 = (M1>>1) + rand()%(M1>>1);
    hash2 = (M2>>1) + rand()%(M2>>1);
}

void CuckooHash::makeSizes(void)
{
    this->M1 = 666013 + rand()%2000111;
    this->M2 = 555017 + rand()%2000111;

    this->M1 += 1-(this->M1 & 1);
    this->M2 += 1-(this->M2 & 1);

}

inline int CuckooHash::rehash(void)
{
    vector<int> aux;
    
    aux.push_back(uninserted);

    for(int i=0;i<M1;i++)
        if(Hash1[i])
            aux.push_back(Hash1[i]);

    for(int i=0;i<M2;i++)
        if(Hash2[i])
            aux.push_back(Hash2[i]);

    int i;

    for(;;)
    {
        //cout << "WTF o.O" << M1 << " " << M2 << " " << aux.size() << "\n";
        makeSizes();
        remakeFunctions();

        for(int i=0;i<M1;i++)
            Hash1[i] = 0;
        for(int i=0;i<M2;i++)
            Hash2[i] = 0;

        for(i=aux.size()-1;i>=0;i--)
            if(!insertTry(aux[i]))
            {
                //cout << "Problem!!!" << aux[i] << "\n";
                break;
            }

        if(i == -1)
            return 1;
    }

    return 0;
}

inline int CuckooHash::reinsert(int x)
{
    int hash_funct2 = hashFunction2(x);

    if(size == 0)
    {
        uninserted = x;
        return 0;
    }

    if(Hash2[hash_funct2])
    {
        int y = Hash2[hash_funct2];
        Hash2[hash_funct2] = x;
        -- size;
        return reinsert(y);
    }
    else
    {
        Hash2[hash_funct2] = x;
        return 1;
    }
}

inline int CuckooHash::insertTry(int x)
{
    int hash_funct1 = hashFunction1(x);

    if(find(x))
    {
        return 1;
    }

    if(Hash1[hash_funct1])
    {
        int y = Hash1[hash_funct1];
        Hash1[hash_funct1] = x;
        size = 14;
        return reinsert(y);
    }
    else
    {
        Hash1[hash_funct1] = x;
        return 1;
    }
}

inline int CuckooHash::insert(int x)
{
    if(!insertTry(x))
    {
//        cout << x;
        rehash();
    }

    return 1;
}

inline int CuckooHash::remove(int x)
{
    int hash_funct1 = hashFunction1(x);
    int hash_funct2 = hashFunction2(x);

    if(Hash1[hash_funct1] == x)
    {
        Hash1[hash_funct1] = 0;
        return 1;
    }
    else if(Hash2[hash_funct2] == x)
    {
        Hash2[hash_funct2] = 0;
        return 1;
    }

    return -1;
}

inline int CuckooHash::find(int x)
{
    int hash_funct1 = hashFunction1(x);
    int hash_funct2 = hashFunction2(x);

    if(Hash1[hash_funct1] == x)
    {
        return 1;
    }
    else if(Hash2[hash_funct2] == x)
    {
        return 1;
    }

    return 0;
}

int SolV[1000002];
char S[10000002];

inline int getInt(int *i)
{
    int rez = 0;

    for(;!isdigit(S[*i]);++(*i));
    for(;isdigit(S[*i]);rez = rez*10+(S[*i])-'0',++(*i));

    return rez;
}

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

    f.getline(S,sizeof(S),EOF);

    int N,op,a,k = 0;
    SolV[0] = 0;

    CuckooHash Hash;

    N = getInt(&k);

    for(int i=1;i<=N;i++)
    {
        op = getInt(&k);
        a = getInt(&k);
        
        switch(op)
        {
            case 1 : Hash.insert(a);
                break;
            case 2 : Hash.remove(a);
                break;
            case 3 : SolV[++SolV[0]] = Hash.find(a);
        }
    }

    for(int i=1;i<=SolV[0];i++)
        g << SolV[i] << "\n";

    f.close();
    g.close();
}