Cod sursa(job #2378710)

Utilizator KemyKoTeo Virghi KemyKo Data 12 martie 2019 16:00:38
Problema Hashuri Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.93 kb
#include<cstdio>
#include <map>

using namespace std;

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;
    FILE *f, *g;
    f = fopen("hashuri.in",  "r");
    g = fopen("hashuri.out", "w");

    fscanf(f, "%d", &n);
    for(i=1; i<=n; ++i){
        fscanf(f, "%d%d", &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:
                fprintf(g, "%d\n", !(M.find(x) == M.end()));
                break;
        }
    }
    return 0;
}