Cod sursa(job #914817)

Utilizator UnforgivenMihai Catalin Botezatu Unforgiven Data 14 martie 2013 14:52:39
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 5.53 kb
#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstdlib>
#include <time.h>
#include <string.h>
#include <string>

using namespace std;

class Hash_Function
{
private:
    int a , b;
    int prim;
    int size;
public:
    Hash_Function(int &);
    void generate();
    int hash_function(int&);
    int hash_function(float&);
    int hash_function(double&);
    int hash_function(char&);
    int hash_function(string&);
};

Hash_Function::Hash_Function(int &max)
{
    prim = 1000000009;
    size = max;
    generate();
}

void Hash_Function::generate()
{
    a = rand() % size;
    b = rand() % size;
}

int Hash_Function::hash_function(char &value)
{
    return (((long long)a + (long long)value * (long long)b ) % (long long)prim) % (long long)size;
}

int Hash_Function::hash_function(int &value)
{
    return (((long long)a + (long long)value * (long long)b ) % (long long)prim) % (long long)size;
}

int Hash_Function::hash_function(float &value)
{
    while ((value - (int)value) != 0)
    {
        value *= 10;
        if (value > size)
        {
            value -= size;
        }
    }
    return (((long long)a + (long long)value * (long long)b ) % (long long)prim) % (long long)size;
}

int Hash_Function::hash_function(double &value)
{
    while ((value - (int)value) != 0)
    {
        value *= 10;
        if (value > size)
        {
            value -= size;
        }
    }
    return (((long long)a + (long long)value * (long long)b ) % (long long)prim) % (long long)size;
}

int Hash_Function::hash_function(string &value)
{
    int key = 0;
    for (int i =0;i<value.size();i++)
    {
        key += (a + (int)(value[i]) * b);
        key %= size;
    }
    return key;
}

template <class Data>
class Hash_Set
{
    int modulo;
    Data *hash1;
    Data *hash2;
    bool *viz1;
    bool *viz2;
    int hash_size;
    Hash_Function *func1;
    Hash_Function *func2;

    public:
    Hash_Set<Data>(int);
    ~Hash_Set<Data>();

    void erase_all();
    bool insert(Data&);
    void erase(Data&);
    bool find(Data&);
    void Build_Hash_From_File(char *, char *);
};

template <class Data>
void Hash_Set<Data>::Build_Hash_From_File(char * file_input , char * file_output)
{
    bool ok = false;
    srand(time(0));

    int mod;
    while (ok == false)
    {

        ifstream input(file_input);
        ofstream output(file_output);
        int n;
        func1->generate();
        func2->generate();
        input >> n;
        for (int i =0;i<n;i++)
        {
            int op;
            Data x;
            input >> op >> x;
            if (op == 1)
            {
                if ( !insert(x) )
                {
                    ok = false;
                    break;
                }
            }
            if (op == 2)
            {
                erase(x);
            }
            if (op == 3)
            {
                output << find(x) << "\n";
            }
        }
        ok = true;
    }
}


int main()
{
    Hash_Set<int> t(1000000);
    t.Build_Hash_From_File("hashuri.in","hashuri.out");
    return 0;
}


template <class Data>
Hash_Set<Data>::Hash_Set(int size)
{
    hash1 = new Data[size];
    hash2 = new Data[size];
    viz1 = new bool[size];
    viz2 = new bool[size];
    hash_size = size;

    func1 = new Hash_Function(size);
    func2 = new Hash_Function(size);

    fill(viz1,viz1+size,false);
    fill(viz2,viz2+size,false);
}

template <class Data>
Hash_Set<Data>::~Hash_Set()
{
    delete[] hash1;
    delete[] hash2;
    delete[] viz1;
    delete[] viz2;
}

template <class Data>
void Hash_Set<Data>::erase_all()
{
    fill(viz1,viz1+hash_size,false);
    fill(viz2,viz2+hash_size,false);
}

template <class Data>
void Hash_Set<Data>::erase(Data &value)
{
     int h1 = func1->hash_function(value);
     int h2 = func2->hash_function(value);
     if (hash1[h1] == value && viz1[h1] == true)
     {
        viz1[h1] = false;
     }
     else if (hash2[h2] == value && viz2[h2] == true)
     {
        viz2[h2] = false;
     }
}

template <class Data>
bool Hash_Set<Data>::find(Data &value)
{
    int h1 = func1->hash_function(value);
    int h2 = func2->hash_function(value);
    if ((hash1[h1] == value && viz1[h1] == true)|| (hash2[h2] == value && viz2[h2] == true))
    {
        return true;
    }
    else
    {
        return false;
    }
}

template <class Data>
bool Hash_Set<Data>::insert(Data &value)
{
    if (find(value)) return true;
    Data init = value;
    char which = 1;
    int iteratii = 0;
    for (int i =0;i<30;i++)
    {
        iteratii++;
        int h1 = func1->hash_function(value);
        int h2 = func2->hash_function(value);
        if (viz1[h1] == false)
        {
            hash1[h1] = value;
            viz1[h1] = true;
            return true;
        }
        else if(viz2[h2] == false)
        {
            hash2[h2] = value;
            viz2[h2] = true;
            return true;
        }
        else
        {
            if (which == 1)
            {
                Data aux = value;
                value = hash1[h1];
                hash1[h1] = aux;
                viz1[h1] = true;
            }
            else
            {
                Data aux = value;
                value = hash2[h2];
                hash2[h2] = aux;
                viz2[h2] = true;
            }
            which = - which;
        }
    }

    return false;
}