Pagini recente » Cod sursa (job #2476400) | Borderou de evaluare (job #2100072) | Cod sursa (job #929678) | Cod sursa (job #1933873) | Cod sursa (job #914084)
Cod sursa(job #914084)
#include<iostream>
#include<fstream>
#include<stdlib.h>
#include<time.h>
#include<string.h>
using namespace std;
class hash{
int *v1;
int *v2;
int size;
int prim;
int a1;
int a2;
char *fin;
char *fout;
public:
hash(char*, char*);
~hash();
void aloca();
void defsize();
int inserare(int x);
void null();
int h1(int x);
int h2(int x);
void reset();
void sterge(int x);
int cautare(int x);
void rehash(int k);
void sp();
};
void hash:: sp(){
srand(time(NULL));
reset();
null();
ifstream f(fin);
ofstream g(fout);
int n,x,op,i;
f >> n;
for(i=1;i<=n;i++){
f>>op>>x;
if(op==1)
{if(inserare(x)==0)rehash(i);}
if(op==2){
if(cautare(x)==1)
sterge(x);
}
if(op==3){
if(cautare(x)==1)
g<<1<<"\n";
else g<<0<<"\n";
}
}
}
int main(){
hash t("hashuri.in","hashuri.out");
t.sp();
return 0;
}
void hash:: defsize(){
size=1000000;
}
hash:: ~hash(){
delete[] v1;
delete[] v2;
delete[] fin;
delete[] fout;
}
void hash:: aloca(){
v1=(int*)calloc(size,sizeof(int));
v2=(int*)calloc(size,sizeof(int));
}
void hash:: null(){
int i;
for(i=0;i<size;i++)
v1[i]=v2[i]=0;
}
int hash:: h1(int x){
int y = ((x*(a1%10)+x%a2)+x*5)%size;
return ((x*(a1%10)+x%a2)+x*5)%size;
}
int hash:: h2(int x){
return (((long long)x*((long long)(a1%10))%prim)+(long long)x*(long long)(a1%10))%size;
}
void hash:: reset(){
a1=rand()%prim;
a2=rand()%prim;
}
int hash:: cautare(int x){
if(v1[h1(x)]==x)return 1;
if(v2[h2(x)]==x)return 1;
return 0;
}
int hash:: inserare(int x){
if(cautare(x)==1)return 1;
int k=0,aux;
while(k<30){
if(v1[h1(x)]==0){
v1[h1(x)]=x;
return 1;}
else if(v2[h2(x)]==0){
v2[h2(x)]=x;
return 1;}
else{
k++;
if(k%2==1){
aux=v1[h1(x)];
v1[h1(x)]=x;
x=aux;}
else{
aux=v2[h2(x)];
v2[h2(x)]=x;
x=aux;}
}
}
return 0;
}
void hash:: sterge(int x){
if(v1[h1(x)]==x)v1[h1(x)]=0;
else if(v2[h2(x)]==x)v2[h2(x)]=0;
}
void hash:: rehash(int k){
reset();
null();
int i,op,x;
ifstream f(fin);
f>>i;
for(i=1;i<=k;i++){
f>>op>>x;
if(op==1)
if(inserare(x)==0)rehash(k);
if(op==2){
if(cautare(x)==1)
sterge(x);
}
}
f.close();
}
hash:: hash(char *in, char *out){
defsize();
aloca();
prim=18593;
fin=new char[strlen(in)];
fout=new char[strlen(out)];
strcpy(fin,in);
strcpy(fout,out);
}