Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <forward_list>
- #include <string>
- #include <vector>
- using namespace std;
- int MyHash(const string& key) {
- int result = 0;
- for (int i = 0; i < min(key.size(), 5ul); ++i)
- result = result * 37 + key[i];
- return result;
- }
- class HashSet {
- public:
- HashSet() : table(80) {}
- bool Has(const string& key) const;
- // Возвращает false, если дубль.
- bool Add(const string& key);
- // Возвращает false, если ключа нет.
- bool Remove(const string& key);
- private:
- vector<forward_list<string>> table;
- };
- bool HashSet::Has(const string& key) const {
- int hash = MyHash(key) % table.size();
- auto& current_list = table[hash];
- auto it = find(current_list.begin(), current_list.end(), key);
- return it != current_list.end();
- }
- bool HashSet::Add(const string& key) {
- int hash = MyHash(key) % table.size();
- auto& current_list = table[hash];
- auto it = find(current_list.begin(), current_list.end(), key);
- if (it != current_list.end()) return false;
- current_list.insert_after(current_list.before_begin(), key);
- return true;
- }
- bool HashSet::Remove(const string& key) {
- int hash = MyHash(key) % table.size();
- auto& current_list = table[hash];
- return current_list.remove(key);
- }
- int main() {
- HashSet hashset;
- char command = 0;
- string value;
- while (cin >> command >> value) {
- switch (command) {
- case '?':
- cout << (hashset.Has(value) ? "OK" : "FAIL") << endl;
- break;
- case '+':
- cout << (hashset.Add(value) ? "OK" : "FAIL") << endl;
- break;
- case '-':
- cout << (hashset.Remove(value) ? "OK" : "FAIL") << endl;
- break;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement