Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <fstream>
- #include <cmath>
- using namespace std;
- std::ifstream input("set.in", std::ios_base::in);
- std::ofstream output("set.out", std::ios_base::out | std::ios_base::trunc);
- struct HashTable {
- HashTable() {
- hastTable.assign(SIZE + 3, vector<int>(0)); // 3 про запас, лень думать
- }
- int getHash(int value) {
- long a = value * 113 + 145861;
- a %= 6130667;
- a %= SIZE;
- return abs(a);
- }
- void insert(int value) {
- if (!contains(value)) {
- int hash = getHash(value);
- hastTable[hash].push_back(value);
- }
- }
- bool contains(int value) {
- int hash = getHash(value);
- if (!hastTable[hash].empty()) {
- for (int i : hastTable[hash]) {
- if (i == value) {
- return true;
- }
- }
- }
- return false;
- }
- void remove(int value) {
- if (contains(value)) {
- int hash = getHash(value);
- for (int &i : hastTable[hash]) {
- if (i == value) {
- i = 1000001;
- }
- }
- }
- }
- const int SIZE = 987911;
- vector<vector<int>> hastTable;
- };
- int main() {
- string command;
- int value;
- HashTable hashtable;
- while (input >> command) {
- input >> value;
- if (command == "insert") {
- hashtable.insert(value);
- } else if (command == "exists") {
- output << (hashtable.contains(value) ? "true" : "false") << "\n";
- } else if (command == "delete") {
- hashtable.remove(value);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement