Pagini recente » Cod sursa (job #560456) | Cod sursa (job #80772) | Cod sursa (job #1006647) | Cod sursa (job #2266323) | Cod sursa (job #927665)
Cod sursa(job #927665)
#include<iostream>
#include<fstream>
#include<stdlib.h>
#include<time.h>
#include<string>
#include<string.h>
#include<stdio.h>
#include<math.h>
using namespace std;
template<class T>
class hash{
T *v1;
T *v2;
int size;
int prim;
int a1;
int a2;
char *fin;
char *fout;
public:
hash(char*, char*);
~hash();
void aloca();
bool operator+=(T x);
void operator-=(T x);
bool operator[](T x);
void null();
int h1(int x);
int h2(int x);
int h1(float x);
int h2(float x);
int h1(char x);
int h2(char x);
int h1(double x);
int h2(double x);
int h1(string x);
int h2(string x);
void reset();
void rehash(int k);
void sp();
};
template<class T>
void hash<T>:: sp(){
srand(time(NULL));
reset();
null();
ifstream f(fin);
ofstream g(fout);
int n,op,i;
T x;
f>>n;
for(i=1;i<=n;i++){
f>>op>>x;
if(op==1)
{if(((*this)+=x)==false)rehash(i);}
if(op==2){
if((*this)[x]==1)
(*this)-=x;
}
if(op==3){
if((*this)[x]==1)
g<<1<<"\n";
else g<<0<<"\n";
}
}
}
int main(){
hash<int> t("hashuri.in","hashuri.out");
t.sp();
return 0;
}
template<class T>
hash<T>:: ~hash(){
delete[] v1;
delete[] v2;
delete[] fin;
delete[] fout;
}
template<class T>
void hash<T>:: aloca(){
v1=(T*)calloc(size,sizeof(T));
v2=(T*)calloc(size,sizeof(T));
size=1000000;
}
template<class T>
void hash<T>:: null(){
int i;
for(i=0;i<size;i++)
v1[i]=v2[i]=0;
}
template <class T>
int hash<T>:: h1(int x){
return (((long long) x*(long long)(a1%10)+(long long)x%a2)+(long long)x*5)%size;
}
template <class T>
int hash<T>:: h2(int x){
return (((long long)x*((long long)(a1%10))%prim)+(long long)x*(long long)(a1%10))%size;
}
template<class T>
int hash<T>:: h1(float x){
int exp;
float result=frexp(x,&exp);
return (((long long) exp*(long long)(a1%10)+(long long)exp%a2)+(long long)exp*5)%size;
}
template<class T>
int hash<T>:: h2(float x){
int exp;
float result=frexp(x,&exp);
return (((long long)exp*((long long)(a1%10))%prim)+(long long)exp*(long long)(a1%10))%size;
}
template <class T>
int hash<T>:: h1(char x){
int y=(int) x;
return (((long long) y*(long long)(a1%10)+(long long)y%a2)+(long long)y*5)%size;
}
template <class T>
int hash<T>:: h2(char x){
int y=(int) x;
return (((long long)y*((long long)(a1%10))%prim)+(long long)y*(long long)(a1%10))%size;
}
template<class T>
int hash<T>::h1(string x){
int suma=0,i,lung;
lung=x.length();
for(i=0;i<lung;i++)
suma=suma+x[i]*i;
return (((long long)suma*(long long)(a1%10)+(long long)suma%a2)+(long long)suma*5)%size;
}
template<class T>
int hash<T>::h2(string x){
int suma=0,i,lung;
lung=x.length();
for(i=0;i<lung;i++)
suma=suma+x[i]*i;
return (((long long)suma*((long long)(a1%10))%prim)+(long long)suma*(long long)(a1%10))%size;
}
template<class T>
void hash<T>:: reset(){
a1=rand()%prim;
a2=rand()%prim;
}
template<class T>
bool hash<T>:: operator[](T x){
if(v1[h1(x)]==x)return 1;
if(v2[h2(x)]==x)return 1;
return 0;
}
template<class T>
bool hash<T>:: operator+=(T x){
if((*this)[x]==1)return true;
int k=0,aux;
while(k<30){
if(v1[h1(x)]==0){
v1[h1(x)]=x;
return true;}
else if(v2[h2(x)]==0){
v2[h2(x)]=x;
return true;}
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 false;
}
template<class T>
void hash<T>:: operator-=(T x){
if(v1[h1(x)]==x)v1[h1(x)]=0;
else if(v2[h2(x)]==x)v2[h2(x)]=0;
}
template<class T>
void hash<T>:: rehash(int k){
reset();
null();
int i,op;
T x;
ifstream f(fin);
f>>i;
for(i=1;i<=k;i++){
f>>op>>x;
if(op==1)
if((*this+=x)==false) rehash(k);
if(op==2){
if((*this)[x]==1)
(*this)-=x;
}
}
f.close();
}
template<class T>
hash<T>:: hash(char *in, char *out){
aloca();
prim=18593;
fin=new char[strlen(in)];
fout=new char[strlen(out)];
strcpy(fin,in);
strcpy(fout,out);
}