Pagini recente » Rating Tulbure Marius (ancalagon_13) | Cod sursa (job #1126457) | Cod sursa (job #1603164) | Cod sursa (job #1461704) | Cod sursa (job #916270)
Cod sursa(job #916270)
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<fstream>
#include<time.h>
#define mod 666013
#define inf 0
using namespace std;
class cuckoo
{
int *H1,*H2,n,r1,r2;
public:
cuckoo(int);
~cuckoo();
int h1(int);
int h2(int);
int Cautare(int);
void Inserare(int,int);
void Stergere(int);
void Hashing();
};
cuckoo :: cuckoo(int x)
{
n=x;
H1=new int[n];
H2=new int[n];
srand(time(NULL));
r1=rand();
r2=rand();
}
cuckoo :: ~cuckoo()
{
delete[] H1;
delete[] H2;
}
int cuckoo :: h1(int x)
{
return x%r1;
}
int cuckoo :: h2(int x)
{
return x%r2;
}
int cuckoo :: Cautare(int x)
{
if(H1[h1(x)]==x || H2[h2(x)]==x)
return 1; //a fost gasit
return 0;
}
void cuckoo :: Inserare(int x,int max)
{
int aux;
if(Cautare(x)==1) return;
while(max)
{
if(H1[h1(x)]==inf) {H1[h1(x)]=x;return;}
aux=x;x=H1[h1(x)];H1[h1(x)]=aux;
if(H2[h2(x)]==inf) {H2[h2(x)]=x;return;}
aux=x;x=H2[h2(x)];H2[h2(x)]=aux;
max--;
}
Inserare(x,max);
}
void cuckoo :: Stergere(int x)
{
if(Cautare(x)==0) return;
if(H1[h1(x)]==x) H1[h1(x)]=inf;
if(H2[h2(x)]==x) H2[h2(x)]=inf;
}
void cuckoo ::Hashing()
{
ifstream f("cuckoo.in");
ofstream g("cuckoo.out");
int n,i,a,nr;
for(int i=0;i<mod;i++)
{H1[i]=inf;H2[i]=inf;}
f>>n;
for(i=1;i<=n;i++)
{
f>>nr>>a;
if(nr==1)
Inserare(a,n);
if(nr==2)
Stergere(a);
if(nr==3)
g<<Cautare(a)<<endl;
}
f.close();
g.close();
}
int main()
{
cuckoo H(mod);
H.Hashing();
return 0;
}