Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <unordered_set>
- #include <vector>
- int Hash(const std::string& key, size_t m) {
- if (key.empty()) return 0;
- return key[0] % m;
- }
- struct HTNode {
- std::string Key;
- HTNode* Next = nullptr;
- explicit HTNode(const std::string& key) : Key(key) {}
- };
- // Хеш-таблица строк, разрешающая коллизии методом цепочек.
- // Has, Add, Remove.
- class HashTable {
- public:
- explicit HashTable(int table_size) : table(table_size, nullptr) {}
- bool Has(const std::string& key) const {
- int hash = Hash(key, table.size());
- HTNode* node = table[hash];
- while (node != nullptr) {
- if (node->Key == key) return true;
- node = node->Next;
- }
- return false;
- }
- bool Add(const std::string& key);
- bool Remove(const std::string& key);
- private:
- std::vector<HTNode*> table;
- };
- bool HashTable::Add(const std::string& key) {
- // Нужно проверить наличие элемента.
- int hash = Hash(key, table.size());
- HTNode* node = table[hash];
- while (node != nullptr) {
- if (node->Key == key) return false;
- node = node->Next;
- }
- HTNode* new_node = new HTNode(key);
- new_node->Next = table[hash];
- table[hash] = new_node;
- return true;
- }
- // 1 -> -> **
- bool HashTable::Remove(const std::string& key) {
- int hash = Hash(key, table.size());
- HTNode* node = table[hash];
- HTNode* prev = nullptr;
- while (node != nullptr) {
- if (node->Key == key) {
- (prev ? prev->Next : table[hash]) = node->Next;
- delete node;
- return true;
- }
- prev = node;
- node = node->Next;
- }
- return false;
- }
- int main1() {
- HashTable table(80);
- char command = 0;
- std::string value;
- while (std::cin >> command >> value) {
- if (command == '+') {
- std::cout << (table.Add(value) ? "OK" : "FAIL") << std::endl;
- } else if (command == '?') {
- std::cout << (table.Has(value) ? "OK" : "FAIL") << std::endl;
- } else if (command == '-') {
- std::cout << (table.Remove(value) ? "OK" : "FAIL") << std::endl;
- }
- }
- return 0;
- }
- int main() {
- /* int* pointer = new int;
- *pointer = 5;
- std::string s;
- s.length();
- std::string* ps = new std::string("HelloWorld");
- std::cout << *ps;
- std::cout << (*ps).length();
- std::cout << ps->length();*/
- std::unordered_set<std::string> table;
- auto it = table.find("Hello");
- it.operator*();
- /* for (auto i = table.begin(); i != table.end(); ++i) {
- std::cout << *it;
- }
- for (const auto& x : table) {
- std::cout << x;
- }*/
- // Новый метод contains есть только в C++20
- char command = 0;
- std::string value;
- while (std::cin >> command >> value) {
- if (command == '+') {
- std::cout << (table.insert(value).second ? "OK" : "FAIL") << std::endl;
- } else if (command == '?') {
- std::cout << (table.count(value) ? "OK" : "FAIL") << std::endl;
- } else if (command == '-') {
- std::cout << (table.erase(value) ? "OK" : "FAIL") << std::endl;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement