Pagini recente » Cod sursa (job #2998769) | Cod sursa (job #1144174) | Cod sursa (job #1485027) | Cod sursa (job #1510607) | Cod sursa (job #918632)
Cod sursa(job #918632)
#include <iostream>
#include <fstream>
#include <cstdlib>
#define DIM1 666013
#define DIM2 650013
#define RANGE 100000
#define REP 50
using namespace std;
class Hash{
private:
int *hash1,*hash2;
int h1,h2;
int state;
void rehash();
bool add2(int,int*,int*,int,int);
public:
Hash();
~Hash();
void add(int);
int find(int);
void del(int);
};
Hash::Hash(){
h1=DIM1; h2=DIM2;
hash1= (int *) calloc(h1,sizeof(int));
hash2= (int *) calloc(h2,sizeof(int));
}
Hash::~Hash(){
free(hash1);
free(hash2);
}
void Hash::add(int x){
if(!find(x)){
if(!add2(x,hash1,hash2,h1,h2)){
rehash(); add(x);
}
}
}
int Hash::find(int x){
if(hash1[x%h1]==x || hash2[x%h2]==x) return 1;
return 0;
}
void Hash::del(int x){
if(hash1[x%h1]==x) hash1[x%h1]=0;
if(hash2[x%h2]==x) hash2[x%h2]=0;
}
void Hash::rehash(){
int *hash1aux, *hash2aux;
int i,h1aux,h2aux;
h1aux=DIM1+rand()%RANGE;
h2aux=DIM2+rand()%RANGE;
hash1aux= (int *) calloc(h2aux,sizeof(int));
hash2aux= (int *) calloc(h2aux,sizeof(int));
for(i=0;i<h1;++i)
if(!add2(hash1[i],hash1aux,hash2aux,h1aux,h2aux)){
free(hash1aux);
free(hash2aux);
rehash();
return;
}
for(i=0;i<h2;++i)
if(!add2(hash2[i],hash1aux,hash2aux,h1aux,h2aux)){
free(hash1aux);
free(hash2aux);
rehash();
return;
}
free(hash1); hash1=hash1aux;
free(hash2); hash2=hash2aux;
h1=h1aux;
h2=h2aux;
}
bool Hash::add2(int x,int * hash1, int * hash2,int h1,int h2){
int aux,i,init=x;
do{
++i;
if(hash1[x%h1]==0){
hash1[x%h1]=x;
return 1;
}
if(hash2[x%h2]==0){
hash2[x%h2]=x;
return 1;
}
aux=hash2[x%h2];
hash2[x%h2]=x;
x=aux;
} while(i<REP && x!=init)
return 0;
}
int main(){
srand(time(NULL));
ifstream f("hashuri.in");
ofstream g("hashuri.out");
int numq,q,nr;
Hash h;
f>>numq;
while(numq>0){
numq--;
f>>q>>nr;
switch(q){
case 1 : h.add(nr); break;
case 2 : h.del(nr); break;
case 3 : g<<h.find(nr)<<'\n'; break;
}
}
}