Cod sursa(job #915273)

Utilizator bogdan93Grigorescu Bogdan bogdan93 Data 14 martie 2013 21:26:07
Problema Hashuri Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 3.74 kb
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <cstring>
#include <cmath>
#include <bitset>
#include <ctime>

using namespace std;

class blablaHashing{
    public:

        blablaHashing(int, string, string);
        ~blablaHashing();

        bool insertNumber(int);
        bool find(int);
        void erase(int);
        void start(bool&);

    private:

        fstream in, out;
        int *table1, *table2;
        int loopLimit, random1,  random2, sizeHash;
        int random3, random4;
        bool *viz1, *viz2;
        //bitset<1000000> viz1, viz2;

        void swap(int &,int &);
        void changeHashingFunction();
        int funct1(int);
        int funct2(int);

};

int main (){

    srand((unsigned)time(NULL));

    blablaHashing Hash(1000111, "hashuri.in", "hashuri.out");

    bool isgood = false;
    Hash.start(isgood);

    return 0;
}

blablaHashing::blablaHashing(int n, string fin, string fout){

    sizeHash = n;
    in.open(fin.c_str(), ios::in);
    out.open(fout.c_str(), ios::out);

    table1 = new int [n];
    table2 = new int [n];
    viz1 = new bool [n];
    viz2 = new bool [n];
    loopLimit = 25;
}

inline void blablaHashing::start(bool &isgood){

    if(!isgood) {

    changeHashingFunction();

    int M, op, x;
    bool ok;
    in >> M;
    for (; M; M--){
        in >> op >> x;
        if (op == 1){
                if(!find(x)) {
                    ok = insertNumber(x);
                    if (!ok) {
                        in.seekg(0, ios::beg);
                        out.seekg(0, ios::beg);
                        isgood = false;
                        start(isgood);
                        return;
                    }
                }
                continue;
        }
        if (op == 2){
            if (find (x))   erase (x);
            continue;
        }

        if (find(x))    out << 1 << endl;
            else out << 0 << endl;
    }

    isgood = true;

    }
}

inline bool blablaHashing::find (int x){

    int func1 = funct1(x);
    int func2 = funct2(x);

    if (table1[func1] == x || table2[func2] == x) return true;
        else return false ;
}

inline void blablaHashing::erase (int x){

    int func1 = funct1(x);
    int func2 = funct2(x);

    if (table1[func1] == x) {
            table1[func1] = 0;
            viz1[func1] = 0;
    }
    else{
        table2[func2] = 0;
        viz2[func2] = 0;
    }
}

inline bool blablaHashing::insertNumber(int x){

    int tempAux1, tempAux2;
    for (int i = 1; i <= loopLimit; i += 2){

        tempAux1 = funct1(x);
        if ( !viz1[tempAux1] ){
            table1[tempAux1] = x;
            return true;
        }
        swap(x, table1[tempAux1]);

        tempAux2 = funct2(x);
        if ( !viz2[tempAux2] ){
            table2[tempAux2] = x;
            return true;
        }
        swap(x, table2[tempAux2]);

    }
    return false;
}

blablaHashing::~blablaHashing(){

    delete []table1;
    delete []table2;
    delete []viz1;
    delete []viz2;

    in.close();
    out.close();
}

void blablaHashing::changeHashingFunction(){

    random1 = rand()%sizeHash;
    random2 = rand()%sizeHash;
    random3 = rand()%sizeHash;
    random4 = rand()%sizeHash;

    memset(viz1, 0, sizeof(viz1));
    memset(viz2, 0, sizeof(viz2));

}
inline int blablaHashing::funct1(int x){

    return ( (long long)x * random1 + (long long) random2) % sizeHash;
}
inline int blablaHashing::funct2(int x){

    return ((long long)x * random3 + (long long)random4) % sizeHash;
}
inline void blablaHashing::swap(int &a,int &b){

    int tempAux = a;
    a = b;
    b = tempAux;
}