Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <string>
- #include <vector>
- #include <math.h>
- #include <list>
- using namespace std;
- template<typename T>
- class Set {
- private:
- const int PRIME_SIZE = 1000001;
- vector<list<T>>* hashTable;
- int hash(T value) const {
- return abs(value % PRIME_SIZE);
- }
- public:
- Set() {
- this->hashTable = new vector<list<T>>(PRIME_SIZE);
- }
- ~Set() {
- delete this->hashTable;
- }
- void insert(T value) {
- int index = hash(value);
- for (auto i : hashTable->at(index))
- if (i == value)
- return;
- hashTable->at(index).push_back(value);
- }
- void erase(T value) {
- int index = hash(value);
- for (auto iter = hashTable->at(index).begin(); iter != hashTable->at(index).end(); iter++) {
- if (*iter == value) {
- hashTable->at(index).erase(iter);
- return;
- }
- }
- }
- bool find(T value) const {
- int index = hash(value);
- for (auto i : hashTable->at(index))
- if (i == value)
- return true;
- return false;
- }
- };
- int main() {
- ifstream ist("set.in");
- ofstream ost("set.out");
- Set<long long> newSet;
- string com;
- long long value;
- while (ist >> com >> value) {
- if (com == "insert")
- newSet.insert(value);
- else if (com == "delete")
- newSet.erase(value);
- else
- ost << (newSet.find(value) ? "true" : "false") << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement