Pagini recente » Cod sursa (job #2502638) | Cod sursa (job #896164) | Cod sursa (job #758323) | Cod sursa (job #679883) | Cod sursa (job #750391)
Cod sursa(job #750391)
// Double hashing
// Ursateanu Irina Mihaela, Grupa 135
#include <fstream>
#include <stdio.h>
#define mod1 666013
#define mod2 100021
#define mod3 100007
using namespace std;
ifstream f("hashuri.in");
ofstream g("hashuri.out");
class hash
{
private:
int *H; // Folosim vectori statici
int size_of_hash;
public:
hash(int size)
{
H=new int[size];
size_of_hash=size;
}
int hash_table(int X,int i);
void insert(int X);
int find(int X);
void erase(int X);
};
int hash::hash_table(int X,int i)
{
return ((X%mod2)+i*(1+X%mod3))%mod1;
}
int hash::find(int X)
{
int i,aux;// aux retine pozitia in vectorul de hash
for(i=0;i<mod1;i++)
{
aux=hash_table(X,i);
if(H[aux]==0)
return -1;
if(H[aux]==X)
return aux;
}
return -1;
}
void hash::insert(int X)
{
int i, aux;// aux retine pozitia
if(find(X)==-1)
for(i=0;i<mod1;i++)
{
aux=hash_table(X,i);
if(H[aux]<=0)
{
H[aux]=X;
return; // Paraseste cand gaseste o pozitie libera
}
}
}
void hash::erase(int X)
{
int aux;
aux=find(X);
if(aux!=-1)
H[aux]=-1;
}
int main()
{
hash object(mod1);
int i,op,x,n;
f>>n;
for(i=1;i<=n;i++)
{
f>>op>>x;
if(op==1)
object.insert(x);
if(op==2)
object.erase(x);
if(op==3)
if(object.find(x)==-1)g<<"0\n";
else g<<"1\n";
}
return 0;
}