Pagini recente » Cod sursa (job #2448448) | Monitorul de evaluare | Cod sursa (job #2553040) | Diferente pentru olimpici intre reviziile 180 si 85 | Cod sursa (job #735095)
Cod sursa(job #735095)
#include <cstdio>
#include <cstdlib>
using namespace std;
// ------------------ STL de mana -------------------------------
template <class T>
class node
{
public:
T key;
node *next;
};
template <class T>
class table
{
node<T> *v;
int size_of_hash;
public:
table<T>();
void insert(T);
void erase(T);
bool empty();
bool find(T);
int size();
node<T>* first();
node<T>* last();
};
template <class T>
table<T>::table()
{
v = NULL;
size_of_hash = 0;
}
template <class T>
bool table<T>::empty()
{
return size_of_hash ? false : true;
}
template <class T>
int table<T>::size()
{
return size_of_hash;
}
template <class T>
node<T>* table<T>::first()
{
return v;
}
template <class T>
node<T>* table<T>::last()
{
node<T> *p;
p=v;
while (p->next != NULL)
p = p->next;
return p;
}
template <class T>
bool table<T>::find(T x)
{
node<T> *p;
if (empty())
return false;
p = first();
while (p != NULL)
{
if (p->key == x)
return true;
p = p->next;
}
return false;
}
template <class T>
void table<T>::insert(T x)
{
node<T> *p,*newnode;
if (find(x))
return;
newnode = new node<T>;
newnode->key = x;
newnode->next = NULL;
if (empty())
{
v = newnode;
}
else
{
p = last();
p->next = newnode;
}
size_of_hash++;
}
template <class T>
void table<T>::erase(T x)
{
node<T> *p, *u;
if (!find(x))
return;
if (size_of_hash == 1)
{
v = NULL;
}
else
{
u = first();
if (u->key == x)
v = v->next;
else
{
p = u->next;
while (p != NULL)
{
if (p->key == x)
{
u->next = p->next;
delete p;
break;
}
u = p;
p = p->next;
}
}
}
size_of_hash--;
}
// ------------------ interfata ------------------------------
template <class T>
class interfata
{
protected:
table<T> *vector;
int prim;
public:
interfata();
virtual int h(T) = 0;
bool insert(T);
bool erase(T);
short int search(T);
};
template <class T>
interfata<T> :: interfata()
{
prim = 640013;
vector = new table<T>[prim];
}
template <class T>
bool interfata<T>::insert(T x)
{
int index = h(x);
vector[index].insert(x);
return true;
}
template <class T>
bool interfata<T>::erase(T x)
{
int index = h(x);
vector[index].erase(x);
return true;
}
template <class T>
short int interfata<T>::search(T x)
{
int index = h(x);
if (vector[index].find(x))
return 1;
return 0;
}
// ------------------ universal hashing ------------
template <class T>
class universal : public interfata<T>
{
//table<T> *vector;
//int prim, a, b, max;
int a, b, max;
public:
universal<T>();
int h(T);
//bool insert(T);
//bool erase(T);
//short int search(T);
};
template <class T>
universal<T>::universal()
{
//prim = 640013;
//vector = new table<T>[prim];
max = 1000003;
a = rand()%4;
b = rand()%4+1;
}
template <class T>
int universal<T>::h(T x)
{
unsigned int index = a*x+b;
index %= (this->prim);
index %= max;
return index;
}
// ------------------ metoda diviziunii ---------------------------
template <class T>
class division : public interfata<T>
{
public:
int h(T);
//bool insert(T);
//bool erase(T);
//short int search(T);
};
template <class T>
int division<T>::h(T x)
{
int index = x%(this->prim);
return index;
}
// ------------------ main -------------------------------
int main ()
{
int n,op,x;
freopen("hashuri.in","r",stdin);
freopen("hashuri.out","w",stdout);
division<int> H;
scanf("%d",&n);
for (; n; --n)
{
scanf("%d%d",&op,&x);
switch (op)
{
case 1:
H.insert(x);
break;
case 2:
H.erase(x);
break;
case 3:
printf("%d\n",H.search(x));
break;
}
}
return 0;
}