Cod sursa(job #2286951)

Utilizator ajeccAjechiloae Eugen ajecc Data 21 noiembrie 2018 01:55:34
Problema Hashuri Scor 30
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.65 kb
#include <bits/stdc++.h>
using namespace std;

#define CC_HASH_TABLE_MAX_SIZE 20000029

#define hash __HASH__
#define GET_16_BITS(d)(*((const unsigned short *) (d)))
static int HtHashFunction(char *Key)
{
    // Paul Hsieh super fast hash
    // Note that I have no idea how this works, but it should avoid collision very well

    if (NULL == Key)
    {
        perror("ERROR in HtHashFunction => check params: ");
        return -1;
    }

    char *key = Key;

    unsigned int len = strlen(key);
    unsigned int hash = len;
    unsigned int rem = len & 3;
    len >>= 2;

    while (len--)
    {
        hash += GET_16_BITS(key);
        unsigned int temp = (GET_16_BITS(key + 2) << 11) ^ hash;
        hash = (hash << 16) ^ temp;
        key += 2 * sizeof(unsigned short);
        hash += hash >> 11;
    }

    // Handle end cases
    switch (rem)
    {
    case 3:
        hash += GET_16_BITS(key);
        hash ^= hash << 16;
        hash ^= key[sizeof(unsigned short)] << 18;
        hash += hash >> 1;
        break;
    case 2:
        hash += GET_16_BITS(key);
        hash ^= hash << 11;
        hash += hash >> 17;
        break;
    case 1:
        hash += *key;
        hash ^= hash << 10;
        hash += hash >> 1;
    default:
        break;
    }

    // Force "avalanching" of final 127 bits
    hash ^= hash << 3;
    hash += hash >> 5;
    hash ^= hash << 4;
    hash += hash >> 17;
    hash ^= hash << 25;
    hash += hash >> 6;

    return hash % CC_HASH_TABLE_MAX_SIZE;
}

int n;
bool in[CC_HASH_TABLE_MAX_SIZE];

ifstream fin("hashuri.in");
ofstream fout("hashuri.out");

int main()
{
    fin >> n;
    while(n--)
    {
        int op;
//
        fin >> op;
        char *elem = NULL;
        int aux;
        fin >> aux;
        int ttemp = aux;
        int ssize = 0;
        deque<char> d;
        while(aux)
        {

            d.push_front((char)(aux % 10) + '0');
            //cout << int(d[0]) << '\n';
            aux /= 10;
        }
        elem = (char*)malloc(sizeof(char) * (d.size() + 1));
        if(NULL == elem)
        {
            cout << "EROROROROR";
        }
        for(int  i = 0; i < d.size(); i++)
        {
            elem[i] = d[i];
           // cout << elem[i] << ' ' << d[i] << '\n';
        }
        elem[d.size()] = '\0';
        //cout << "elem = ";

        int hash = HtHashFunction(elem);
        //cout << hash << '\n';

        if(op == 1)
        {
            in[hash] = 1;
        }
        else if(op == 2)
        {
            in[hash] = 0;
        }
        else fout << in[hash] << '\n';
    }

    return 0;
}