Pagini recente » Cod sursa (job #2062047) | Cod sursa (job #1677692) | Cod sursa (job #2751107) | Cod sursa (job #1783507) | Cod sursa (job #714756)
Cod sursa(job #714756)
//hashing with open adresing
//h(x) = (h'(x) + r1 * i + r2 * i^2) % M
#include<iostream>
#include<fstream>
#include<cstdlib>
using namespace std;
const int nmax = 1000003;
int v[nmax],n;
int r1,r2;//random
inline int hashing(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[hashing(x,k)]!=0 && v[hashing(x,k)]!=-1 && k<=nmax) k++;
if (k>=n+1)
{
cout<<"Vectorul este plin!";
return;
}
v[hashing(x,k)]=x;
}
void stergere (int x)
{
int k=0;
while (v[hashing(x,k)]!=x && v[hashing(x,k)]!=0 && k<=nmax) k++;
if (hashing(x,k)==0) return;
else if (k<=n) v[hashing(x,k)]=-1;
}
bool cautare (int x)
{
int k=0;
while (v[hashing(x,k)]!=x && v[hashing(x,k)]!=0 && k<=nmax) k++;
if (k<=n && v[hashing(x,k)]==x) return 1;
else return 0;
}
int main ()
{
r1=rand()%nmax;
r2=rand()%nmax;
ifstream fin ("hashuri.in");
ofstream fout ("hashuri.out");
int optiune,x;
fin>>n;
for (int i=1;i<=n;i++)
{
fin>>optiune>>x;
if (optiune==1) inserare(x);
else if (optiune==2) stergere(x);
else fout<<cautare(x)<<'\n';
}
fin.close();
fout.close();
return 0;
}