Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- const int MAX = 1e6 + 1;
- struct HashSet
- {
- vector <int> v[MAX];
- HashSet()
- {
- for (int i = 0; i < v->size(); ++i)
- v[i] = {};
- }
- int HashFunc(int x)
- {
- return (abs(x) % MAX);
- }
- bool exists(int x)
- {
- int hash = this->HashFunc(x);
- for (auto i : (this->v[hash]))
- if (i == x)
- return 1;
- return 0;
- }
- void insert(int x)
- {
- if (this->exists(x))
- return;
- v[HashFunc(x)].push_back(x);
- }
- void erase(int x)
- {
- if (!this->exists(x))
- return;
- int k = 0;
- for (int i = 0; i < v[HashFunc(x)].size(); ++i)
- if (v[HashFunc(x)][i] == x)
- k = i;
- swap(v[HashFunc(x)][k], v[HashFunc(x)][v[HashFunc(x)].size() - 1]);
- v[HashFunc(x)].pop_back();
- }
- };
- int main()
- {
- freopen("set.in", "r", stdin);
- freopen("set.out", "w", stdout);
- string str;
- int x;
- auto h = new HashSet();
- while (cin >> str)
- {
- cin >> x;
- if (str == "insert")
- h->insert(x);
- else if (str == "delete")
- h->erase(x);
- else if (str == "exists")
- {
- if (h->exists(x))
- cout << "true" << '\n';
- else
- cout << "false" << '\n';
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement