Pagini recente » Cod sursa (job #1881051) | Cod sursa (job #1735622) | Cod sursa (job #2689421) | Cod sursa (job #2562228) | Cod sursa (job #1477470)
#include <bits/stdc++.h>
using namespace std;
class LinearProbing
{
private:
static const int MAX_SIZE = 1 << 20;
static const int MOD = MAX_SIZE - 1;
constexpr static const double A = 0.6180339887; ///A = (sqrt(5) - 1) / 2
static const int EMPTY = -1;
static const int ERASED = -2;
int *table;
public:
LinearProbing() : table(new int [MAX_SIZE]) {
for ( int i = 0; i < MAX_SIZE; ++i )
table[i] = EMPTY;
}
~LinearProbing()
{
delete[] table;
}
int fHash(const int key) /// f(k) = [MAX_SIZE * {k * A}], {k * A} = k * A - [k * A];
{
double k = 1.0 * key * A - (int)(1.0 * key * A);
return (int)(1.0 * k * MAX_SIZE);
}
void insert(const int key)
{
int p = fHash(key);
while (table[p] != EMPTY && table[p] != ERASED)
p = (p + 1) & MOD;
table[p] = key;
}
bool find(const int key)
{
int p = fHash(key);
while (table[p] != EMPTY && table[p] != key)
p = (p + 1) & MOD;
return table[p] == key;
}
void erase(const int key)
{
int p = fHash(key);
while (table[p] != EMPTY && table[p] != key)
p = (p + 1) & MOD;
if (table[p] == key)
table[p] = ERASED;
}
};
LinearProbing L;
int main()
{
ifstream in("hashuri.in");
ofstream out("hashuri.out");
ios_base::sync_with_stdio( false );
int N;
in >> N;
while ( N-- )
{
int tip, x;
in >> tip >> x;
switch(tip)
{
case 1: L.insert(x); break;
case 2: L.erase(x); break;
case 3: out << L.find(x) << "\n"; break;
default: cerr << "Error\n";
}
}
return 0;
}