Pagini recente » Cod sursa (job #2791856) | Cod sursa (job #838091) | Istoria paginii utilizator/ubb_cioba_zait_vasilut | Istoria paginii utilizator/vipioana | Cod sursa (job #2287079)
#include <bits/stdc++.h>
using namespace std;
#define CC_HASH_TABLE_MAX_SIZE 500029
#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;
vector<int> in[CC_HASH_TABLE_MAX_SIZE];
ifstream fin("hashuri.in");
ofstream fout("hashuri.out");
int check_in(int hash, int nr)
{
for(auto i: in[hash]) if(i == nr) return 1;
return 0;
}
int main()
{
fin >> n;
while(n--)
{
int op;
//
fin >> op;
char *elem = NULL;
int aux;
fin >> aux;
int nr = 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)
{
if(!check_in(hash, nr))
{
in[hash].push_back(nr);
}
}
else if(op == 2)
{
if(check_in(hash, nr))
{
//erase(in[hash].begin(), in[hash].end(), nr);
in[hash].erase(remove(in[hash].begin(), in[hash].end(), nr), in[hash].end());
}
}
else fout << check_in(hash, nr) << '\n';
}
return 0;
}