-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashTable.cpp
More file actions
74 lines (65 loc) · 1.15 KB
/
hashTable.cpp
File metadata and controls
74 lines (65 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <unordered_set>
#include <time.h>
#include <limits.h>
using namespace std;
class Hasher{
private:
int n,p,a,b;
int randomAB(int Min,int Max)
{
return Min+rand()%(Max-Min+1);
}
public:
Hasher(int arr_size){
n=arr_size;
p=(n%2==0) ? n+1 : n+2;
while (true){
bool isPrime=true;
for (int i=2;i<=p;++i)
{
if (p%i==0){
isPrime=false;
break;
}
if (isPrime=true) break;
else p+=2;
}
}
srand(time(NULL));
a=randomAB(1,p);
b=randomAB(1,p);
}
int hash(int x){
return (((a*x+b)%p)%n);}
};
int main()
{
int n;
cin>>n;
long long* arr=new long long[n];
for(int i=0;i<n;i++)
{
a[i]=INT_MIN;
}
Hasher h(n);
long long temp;
for(int i=0;i<n;i++){
cin>>temp;
int idx=h.hash(temp);
while(arr[idx]!=INT_MIN && arr[idx]!=temp)
{
idx++;
if (idx=n) idx=0;
}
arr[idx]=temp;
}
int cntr;
for(int i=0;i<n;i++)
{
if (arr[i]!=INT_MIN)
cntr++;
}
cout<<cntr;
return 0;
}