Cod sursa(job #906648)

Utilizator UnforgivenMihai Catalin Botezatu Unforgiven Data 6 martie 2013 23:24:32
Problema Hashuri Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 3.31 kb
#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstdlib>
#include <time.h>

using namespace std;

class Hash_Set
{
    int modulo;
    int *hash1;
    int *hash2;
    int hash_size;
    int va1;
    int vb1;
    int va2;
    int vb2;
    int prim;


    public:
    Hash_Set(int size)
    {
        hash1 = new int[size];
        hash2 = new int[size];
        hash_size = size;
        prim = 999983;
        fill(hash1,hash1+size,0);
        fill(hash2,hash1+size,0);
    }

    bool insert(int&);
    void erase(int&);
    bool find(int&);
    int hash_function1(int&);
    int hash_function2(int&);
    void set_modulo(int& , int& ,int& , int&);
};

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

    int mod;
    while (ok == false)
    {

        ifstream input("hashuri.in");
        ofstream output("hashuri.out");
        Hash_Set t(1000000);
        int a = rand() % 999983 + 1;
        int b = rand() % 999983 + 1;
        int c = rand() % 999983 + 1;
        int d = rand() % 999983 + 1;
        int n;
        t.set_modulo(a,b,c,d);
        input >> n;
        for (int i =0;i<n;i++)
        {
            int x , op;
            input >> op >> x;
            if (op == 1)
            {
                if ( !t.insert(x) )
                {
                    ok = false;
                    break;
                }
            }
            if (op == 2)
            {
                t.erase(x);
            }
            if (op == 3)
            {
                output << t.find(x) << "\n";
            }
        }
        ok = true;
    }
}


int main()
{
    DO_HASH();
    return 0;
}

void Hash_Set::set_modulo(int &a1 , int &b1 , int &a2 , int &b2)
{
    va1 = a1;
    vb1 = b1;
    va2 = a2;
    vb2 = b2;
}

int Hash_Set::hash_function1(int &value)
{
    return (( va1 + value * vb1 ) % prim) % hash_size;
}

int Hash_Set::hash_function2(int &value)
{
    return (( va2 + value * vb2) % prim) % hash_size;
}

void Hash_Set::erase(int &value)
{
     int h1 = hash_function1(value);
     int h2 = hash_function2(value);
     if (hash1[h1] == value)
     {
        hash1[h1] = 0;
     }
     else if (hash2[h2] == value)
     {
        hash2[h2] = 0;
     }
}

bool Hash_Set::find(int &value)
{
    int h1 = hash_function1(value);
    int h2 = hash_function2(value);
    if (hash1[h1] == value || hash2[h2] == value)
    {
        return true;
    }
    else
    {
        return false;
    }
}

bool Hash_Set::insert(int &value)
{
    if (find(value)) return true;
    int init = value;
    char which = 1;
    int iteratii = 0;
    while (value != 0)
    {
        iteratii++;
        int h1 = hash_function1(value);
        int h2 = hash_function2(value);
        if (hash1[h1] == 0)
        {
            hash1[h1] = value;
            value = 0;
        }
        else if(hash2[h2] == 0)
        {
            hash2[h2] = value;
            value = 0;
        }
        else
        {
            if (which == 1)
            {
                int aux = value;
                value = hash1[h1];
                hash1[h1] = aux;
            }
            else
            {
                int aux = value;
                value = hash2[h2];
                hash2[h2] = aux;
            }
            which = - which;
        }
        if (iteratii > 30)
        {
            return false;
        }
    }

    return true;
}