-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask3.cpp
More file actions
178 lines (167 loc) · 5.22 KB
/
task3.cpp
File metadata and controls
178 lines (167 loc) · 5.22 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <iostream>
#include <vector>
#include <random>
#include <limits>
#include <utility>
#include <algorithm>
#include <string>
const long long primeNumber = 2147483053;
class HashFunction
{
public:
HashFunction() {}
HashFunction(long long linearCoefficient, long long constant, long long module,
long long primeModule = primeNumber)
{
linearCoefficient_ = linearCoefficient;
constant_ = constant;
primeModule_ = primeModule;
module_ = module;
}
long long operator()(int number) const
{
long long result = static_cast<long long>(number) % primeModule_;
if (result < 0) {
result += primeModule_;
}
return ((linearCoefficient_ * result + constant_) % primeModule_) % module_;
}
private:
long long linearCoefficient_;
long long constant_;
long long primeModule_;
long long module_;
};
class NoCollisionsQuadraticMemoryHashTable
{
public:
NoCollisionsQuadraticMemoryHashTable() {}
void initialize(const std::vector<int>& data)
{
if (data.empty()) {
return;
}
std::random_device randomDevice;
std::mt19937 generator(randomDevice());
std::uniform_int_distribution<long long> firstDistribution(1, primeNumber);
std::uniform_int_distribution<long long> secondDistribution(0, primeNumber);
long long size = static_cast<long long>(data.size() * data.size());
elements_.resize(size);
bool stop = false;
while (!stop) {
hashFunction_ = HashFunction(firstDistribution(generator),
secondDistribution(generator), size);
std::fill(elements_.begin(), elements_.end(), std::numeric_limits<int>::max());
bool again = false;
for (size_t i = 0; i < data.size(); ++i) {
long long index = hashFunction_(data[i]);
if (elements_[index] == std::numeric_limits<int>::max()) {
elements_[index] = data[i];
} else {
again = true;
break;
}
}
if (!again) {
stop = true;
}
}
}
bool contains(int element) const
{
if (elements_.empty()) {
return false;
}
return elements_[hashFunction_(element)] == element;
}
private:
std::vector<int> elements_;
HashFunction hashFunction_;
};
class FixedSet
{
public:
FixedSet() {}
void initialize(const std::vector<int>& data)
{
std::vector<std::vector<int>> numbers(data.size());
std::random_device randomDevice;
std::mt19937 generator(randomDevice());
std::uniform_int_distribution<long long> firstDistribution(1, primeNumber - 1);
std::uniform_int_distribution<long long> secondDistribution(0, primeNumber - 1);
long long size = static_cast<long long>(data.size());
bool stop = false;
while (!stop) {
hashFunction_ = HashFunction(firstDistribution(generator),
secondDistribution(generator), size);
for (size_t i = 0; i < numbers.size(); ++i) {
numbers[i].resize(0);
}
for (size_t i = 0; i < data.size(); ++i) {
long long index = hashFunction_(data[i]);
numbers[index].push_back(data[i]);
}
long long sumOfSquaresOfLength = 0;
for (size_t i = 0; i < numbers.size(); ++i) {
sumOfSquaresOfLength += static_cast<long long>(numbers[i].size())
* static_cast<long long>(numbers[i].size());
}
if (sumOfSquaresOfLength < 3 * static_cast<long long>(numbers.size())) {
stop = true;
}
}
for (size_t i = 0; i < numbers.size(); ++i) {
hashTables_.push_back(NoCollisionsQuadraticMemoryHashTable());
hashTables_[i].initialize(numbers[i]);
}
}
bool contains(int element) const
{
if (hashTables_.empty()) {
return false;
}
long long index = hashFunction_(element);
return hashTables_[index].contains(element);
}
private:
HashFunction hashFunction_;
std::vector<NoCollisionsQuadraticMemoryHashTable> hashTables_;
};
std::vector<int> readNumbers()
{
int amountOfNumbers;
std::cin >> amountOfNumbers;
std::vector<int> numbers(amountOfNumbers);
for (int& number : numbers) {
std::cin >> number;
}
return numbers;
}
std::vector<bool> processRequests(const std::vector<int>& requests, const FixedSet& fixedSet)
{
std::vector<bool> result;
for (int request : requests) {
result.push_back(fixedSet.contains(request));
}
return result;
}
void printResponses(const std::vector<bool>& result)
{
for (bool answer : result) {
if (answer) {
std::cout << "Yes\n";
} else {
std::cout << "No\n";
}
}
}
int main()
{
std::vector<int> numbers = readNumbers();
std::vector<int> requests = readNumbers();
FixedSet fixedSet;
fixedSet.initialize(numbers);
std::vector<bool> result = processRequests(requests, fixedSet);
printResponses(result);
return 0;
}