Pagini recente » Cod sursa (job #1873995) | Cod sursa (job #493630) | Cod sursa (job #2857598) | Cod sursa (job #461664) | Cod sursa (job #718645)
Cod sursa(job #718645)
//hashing with open adressing
//fara var globale & clasa
//h(x) = (h'(x) + r1 * i + r2 * i^2) % M
//Florea Daniel-Ciprian, gr. 135.
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<cstring>
using namespace std;
class Hash
{
private:
int r1,r2;
static const int nmax=1000003;
int *v;
int n;
public:
Hash (int nr)
{
n=nr;
//vectorul
v = new int [nmax]; //nu pot declara static un vector mare
memset (v,0,sizeof(v)*nmax);
//initializam 2 valori random pt functia de hash
srand(time(NULL));
r1=rand()%nmax;
r2=rand()%nmax;
}
~Hash ()
{
delete[] v;
}
inline int has (int x, int i)
{
int rez=abs((x%nmax+r1*i+r2*i*i))%nmax;
return rez;
}
void inserare (int x)
{
int k=0;
while (v[has(x,k)]!=0 && v[has(x,k)]!=-1 && k<=nmax)
k++;
if (k>=n+1)
{
cout<<"Vectorul este plin!";
return;
}
v[has(x,k)]=x;
}
void stergere (int x)
{
int k=0;
while (v[has(x,k)]!=x && v[has(x,k)]!=0 && k<=nmax) k++;
if (has(x,k)==0) return;
else if (k<=n) v[has(x,k)]=-1;
}
bool cautare (int x)
{
int k=0;
while (v[has(x,k)]!=x && v[has(x,k)]!=0 && k<=nmax) k++;
if (k<=n && v[has(x,k)]==x) return 1;
else return 0;
}
};
int main ()
{
ifstream fin ("hashuri.in");
ofstream fout ("hashuri.out");
int op,x,n;
fin>>n;
Hash h(n);
for (int i=1;i<=n;i++)
{
fin>>op>>x;
if (op==1) { if (!h.cautare(x)) h.inserare(x); }
else if (op==2) h.stergere(x);
else if (op==3) fout<<h.cautare(x)<<'\n';
}
fin.close();
fout.close();
return 0;
}