Cod sursa(job #906730)

Utilizator blechereyalBlecher Eyal blechereyal Data 7 martie 2013 02:49:30
Problema Hashuri Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 3.05 kb
#include <math.h>
#include <fstream>
#include <cstdio>
#include <cstdlib>
#include <time.h>
#define prim 666013

using namespace std;

class cuckoo_set
{
    int mod;
    int *hash;
    int hash_size;
    int a,b;
    public:    cuckoo_set(int size)


    {
        hash = new int[size];
        hash_size = size;
        fill(hash,hash+size,0);
    }

    int insert(int value)
    {
    int cycles=0,aux=value;
    if (find(value)) return 1;
    while (value != 0)
    {
        int poz1 = function1(value);
        int poz2 = function2(value);
        cycles++;
        if (hash[poz1] == 0)
        {
            hash[poz1] = value;
            value = 0;
        }
        else if(hash[poz2] == 0)
        {
            hash[poz2] = value;
            value = 0;
        }
        else
        {
            if (aux == hash[poz1])
            {
                aux = value;
                value = hash[poz1];
                hash[poz1] = aux;
            }
            else if (aux == hash[poz2])
            {
                aux = value;
                value = hash[poz2];
                hash[poz2] = aux;
            }
        }
        if (cycles > log2(this->hash_size))
        {
            return 0;
        }
    }

    return 1;
    }

    void erase(int value)
    {

        int poz1 = function1(value);
        int poz2 = function2(value);
        if (hash[poz1] == value)
            {
            hash[poz1] = 0;
            }
        else if (hash[poz2] == value)
            {
            hash[poz2] = 0;
            }
    }

    int find(int value)
    {
    int poz1 = function1(value);
    int poz2 = function2(value);
    if (hash[poz1] == value || hash[poz2] == value)
    {
        return 1;
    }
    else
    {
        return 0;
    }
    }

    int function1(int value)
    {
        return   (((long long)value * (long long)a )) % (long long)prim % (long long)hash_size;
    }
    int function2(int value)
    {
        return (((long long)value * (long long)b ) % (long long)prim) %(long long)hash_size;
    }
    void make_hash_function()
    {
         a = rand() % hash_size;
         b = rand() % hash_size;
    }
};

int main()
{

    bool ok = false;
    srand(time(0));
    while (ok == false)
    {

        ifstream f("hashuri.in");
        ofstream g("hashuri.out");
        cuckoo_set t(1000000);
        int N;
        t.make_hash_function();
        f >> N;
        for (int i =0;i<N;i++)
        {
            int x , op;
            f >> op >> x;
            if (op == 1)
            {
                if ( !t.insert(x) )
                {
                    ok = false;
                    f.close();
                    g.close();
                    break;
                }
            }
            if (op == 2)
            {
                t.erase(x);
            }
            if (op == 3)
            {
                g << t.find(x) << "\n";
            }
        }
        ok = true;
    }
    return 0;
}