Pagini recente » Cod sursa (job #2933149) | Cod sursa (job #1782978) | Cod sursa (job #2920393) | Cod sursa (job #1523582) | Cod sursa (job #739962)
Cod sursa(job #739962)
//cuckoo hashing
#include<iostream>
#include<fstream>
#include<cstring>
#define PRIM1 666667
#define PRIM2 666013
using namespace std;
ifstream f("hashuri.in");
ofstream g("hashuri.out");
class hash{
int *T1,*T2;
int hash1_size,hash2_size;
public:
hash(int, int);
int h1(int);
int h2(int);
bool cauta(int);
void insert(int,int);
void sterge(int);
};
hash::hash(int size1, int size2){
hash1_size=size1;
hash2_size=size2;
T1=new int[hash1_size];
memset(T1,0,sizeof(T1)*hash1_size);
T2=new int[hash2_size];
memset(T2,0,sizeof(T2)*hash2_size);
}
int hash::h1(int k){
return k%PRIM1;
}
int hash::h2(int k){
return k%PRIM2;
}
bool hash::cauta(int k){
if(T1[h1(k)]==k||T2[h2(k)]==k)
return 1;
return 0;
}
void hash::sterge(int k){
int pos;
pos=h1(k);
if(T1[pos]==k){
T1[pos]=0;
return ;
}
pos=h2(k);
if(T2[pos]==k){
T2[pos]=0;
return ;
}
}
void hash::insert(int k,int ciclare){
if(ciclare==1000){
cout<<"prea multe ciclari\n";
return ;
}
int pos,y,z;
pos=h1(k);
if(T1[pos]==0){
T1[pos]=k;
return ;
}
else{
y=T1[pos];
T1[pos]=k;
pos=h2(y);
if(T2[pos]==0){
T2[pos]=y;
return ;
}
else{
z=T2[pos];
T2[pos]=y;
insert(z,ciclare+1);
}
}
}
int main(){
hash ob(PRIM1,PRIM2);
int n,x;
short int op;
f>>n;
for(int i=1;i<=n;i++){
f>>op>>x;
if(op==1)
if(ob.cauta(x)==0)
ob.insert(x,0);
if(op==2)
ob.sterge(x);
if(op==3)
if(ob.cauta(x)!=0)
g<<"1\n";
else
g<<"0\n";
}
return 0;
}