Pagini recente » Cod sursa (job #2824619) | Cod sursa (job #1435105) | Cod sursa (job #600080) | Cod sursa (job #2219571) | Cod sursa (job #609624)
Cod sursa(job #609624)
#include <fstream>
#include <list>
#include <cmath>
#define uint32 unsigned int
#define uint64 unsigned long long
class HashTable
{
private: const static uint32 p = 16;
private: const static uint32 HASHTABLE_SIZE = 1 << p;
private: std::list<uint32> hash[HASHTABLE_SIZE];
private: const static uint32 w = sizeof(uint32) * 8;
private: const static uint32 a = floor((sqrt(5.0) - 1)/2 * ( 1 << (w - 1) ));
private: uint32 f(const uint32& x)
{
return (uint64)x * a >> (2*w - p);
}
public: void insert(const uint32& x)
{
if(!search(x))
hash[f(x)].push_back(x);
}
public: void remove(const uint32& x)
{
if(search(x))
hash[f(x)].remove(x);
}
public: bool search(const uint32& x)
{
for(std::list<uint32>::iterator I = hash[f(x)].begin(); I != hash[f(x)].end(); ++I)
{
if(*I == x)
return true;
}
return false;
}
};
int main()
{
std::ifstream f("hashuri.in");
std::ofstream g("hashuri.out");
uint32 ops; f >> ops;
HashTable * p = new HashTable;
while(ops--)
{
char type; uint32 num; f >> type >> num;
switch(type)
{
case '1':
p->insert(num);
break;
case '2':
p->remove(num);
break;
case '3':
g << ( p->search(num) ? 1 : 0 ) << '\n';
break;
default:
break;
}
}
delete p;
return 0;
}