-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcountMinSketch.cpp
More file actions
82 lines (81 loc) · 1.9 KB
/
countMinSketch.cpp
File metadata and controls
82 lines (81 loc) · 1.9 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
75
76
77
78
79
80
81
82
#include <iostream>
#include <string>
using namespace std;
#define ll long long
int hash1(string s, int arrSize)
{
ll h = 0;
for (int i = 0; i < s.size(); i++)
{
h += (int)s[i];
h = h % arrSize;
}
return h;
}
int hash2(string s, int arrSize)
{
ll h = 4;
for (int i = 0; i < s.size(); i++)
{
h *= ((int)s[i] + 1) + 42432;
h = h % arrSize;
}
return h;
}
int hash3(string s, int arrSize)
{
ll h = 4;
for (int i = 0; i < s.size(); i++)
{
h *= (((int)s[i] + 1) / 23) * 45 + 45532;
h = h % arrSize;
}
return h % arrSize;
}
int hash4(string s, int arrSize)
{
ll h = 0;
for (int i = 0; i < s.size(); i++)
{
h += (((int)s[i] + 1) / 23) * (h + i + 1);
h = h % arrSize;
}
return h % arrSize;
}
int findFreq(int bitarr[4][100], int arrSize, string s)
{
int a = hash1(s, arrSize);
int b = hash2(s, arrSize);
int c = hash3(s, arrSize);
int d = hash4(s, arrSize);
return min(min(bitarr[0][a], bitarr[1][b]), min(bitarr[2][c], bitarr[3][d]));
}
void insertInTable(int bitarr[4][100], int arrSize, string s)
{
int a = hash1(s, arrSize);
int b = hash2(s, arrSize);
int c = hash3(s, arrSize);
int d = hash4(s, arrSize);
bitarr[0][a]++;
bitarr[1][b]++;
bitarr[2][c]++;
bitarr[3][d]++;
cout << s << " is inserted\n";
}
int main()
{
// ios_base::sync_with_stdio(0);
// cin.tie(0);
// cout.tie(0);
int bitarr[4][100] = {0};
int arrSize{100};
string sarray[7] = {"A", "B", "K", "A", "A", "K", "S"};
for (int i = 0; i < 7; i++)
{
insertInTable(bitarr, arrSize, sarray[i]);
}
cout << "Frequency of A: " << findFreq(bitarr, arrSize, "A");
cout << "Frequency of K: " << findFreq(bitarr, arrSize, "K");
cout << endl;
return 0;
}