Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <iostream>
- #include <string>
- #include <vector>
- using namespace std;
- int Hash(const string& key) {
- return key.empty() ? 0 : key.back();
- }
- class HashTable {
- public:
- HashTable() : table_(8) {}
- bool Add(const string& key) {
- int h = Hash(key) % table_.size();
- auto& v = table_[h];
- auto it = find(v.begin(), v.end(), key);
- if (it != v.end()) return false;
- v.push_back(key);
- return true;
- }
- bool Remove(const string& key) {
- int h = Hash(key) % table_.size();
- auto& v = table_[h];
- auto it = find(v.begin(), v.end(), key);
- if (it == v.end()) return false;
- v.erase(it);
- return true;
- }
- bool Has(const string& key) {
- int h = Hash(key) % table_.size();
- auto& v = table_[h];
- auto it = find(v.begin(), v.end(), key);
- return it != v.end();
- }
- private:
- vector<vector<string>> table_;
- };
- int main() {
- HashTable t;
- string command, key;
- while (cin >> command >> key) {
- if (command == "+") {
- cout << (t.Add(key) ? "OK" : "FAIL") << "\n";
- } else if (command == "-") {
- cout << (t.Remove(key) ? "OK" : "FAIL") << "\n";
- } else if (command == "?") {
- cout << (t.Has(key) ? "OK" : "FAIL") << "\n";
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement