Cod sursa(job #2378705)

Utilizator KemyKoTeo Virghi KemyKo Data 12 martie 2019 15:56:02
Problema Hashuri Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.86 kb
#include <fstream>
#include <map>

using namespace std;

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

unsigned int getHash(int x)
{
    //Return hash value of x as the pos in the hash table
    unsigned int hs = 5381;

    while(x > 0){
        hs = ((hs << 5) + hs) + ((x % 10) + 33); /* hs * 33 + (x % 10) */
        x /= 10;
    }

    return hs;
}

int n, op, x;
map <unsigned int, int> M;

int main()
{
    int i;

    f >> n;
    for(i=1; i<=n; ++i){
        f >> op >> x;
        switch(op){
            case 1:
                if(M.find(x) == M.end())
                    M.insert({x, getHash(x)});
                break;
            case 2:
                M.erase(x);
                break;
            case 3:
                g << !(M.find(x) == M.end()) << '\n';
                break;
        }
    }
    return 0;
}