Pagini recente » Cod sursa (job #220524) | Cod sursa (job #3173796) | Cod sursa (job #1310357) | Cod sursa (job #1971368) | Cod sursa (job #3325558)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("hashuri.in");
ofstream fout("hashuri.out");
template <typename T>
struct HashFunction
{
int operator()(const T& key, int N) const
{
return 0;
}
};
template <>
struct HashFunction<int>
{
long long splitmix64(long long x) const
{
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
int operator()(const int& x, int N) const
{
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return (splitmix64(x + FIXED_RANDOM) % N + N) % N;
}
};
template <>
struct HashFunction<string>
{
int operator()(const string& x, int N) const
{
long long p = 1, nr = 0;
for (char c : x)
{
nr = (nr + (c - 'a' + 1) * p) % N;
p = (p * 137) % N; // Baza 137
}
return nr;
}
};
template <typename T>
class Hash
{
public:
const int n = (1<<16)-1;
vector<vector<T>> H;
HashFunction<T> hasher;
int getIndex(const T& x)
{
return hasher(x, n);
}
void Insert(T x)
{
int index = getIndex(x);
for(const auto& val : H[index])
if(val == x) return;
H[index].push_back(x);
}
void Delete(T x)
{
int index = getIndex(x);
for (size_t i = 0; i < H[index].size(); i++)
{
if (H[index][i] == x)
{
H[index][i] = H[index].back();
H[index].pop_back();
return;
}
}
}
bool Find(T x)
{
int index = getIndex(x);
for (const auto& val : H[index])
{
if (val == x) return true;
}
return false;
}
void Afisare()
{
for (int i = 0; i < n; i++)
{
if (!H[i].empty())
{
cout << i << ": ";
for (const auto& val : H[i]) cout << val << " ";
cout << "\n";
}
}
}
};
class Hashuri : public Hash<int>
{
public:
Hashuri() : Hash() { }
void Rezolvare()
{
int op, n_ops, x;
fin >> n_ops;
while(n_ops--)
{
fin >> op >> x;
if (op == 1) Insert(x);
else if (op == 2) Delete(x);
else fout << Find(x) << "\n";
}
}
};
int main()
{
ios_base::sync_with_stdio(0);
fin.tie(0);
fout.tie(0);
Hashuri A;
A.Rezolvare();
fin.close();
fout.close();
return 0;
}