Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <forward_list>
- using namespace std;
- static const int hash_map_size = 10003;
- int polynom_hash(const string& key) {
- const int a = 123;
- int h = key[0];
- for (int i = 1; i < key.size(); ++i) {
- h = (key[i] + a * h) % hash_map_size;
- }
- return h;
- }
- int main() {
- vector <forward_list <pair<string, int>>> v(hash_map_size);
- int number_of_operations;
- cin >> number_of_operations;
- for (int i = 0; i < number_of_operations; ++i) {
- string command;
- string key;
- cin >> command >> key;
- auto& chain = v[polynom_hash(key)];
- bool operation_complete = false;
- if (command == "put") {
- int value;
- cin >> value;
- for (auto& elem : chain) {
- if (elem.first == key) {
- elem.second = value;
- operation_complete = true;
- break;
- }
- }
- if (!operation_complete) {
- chain.emplace_front(key, value);
- }
- continue;
- }
- if (command == "get") {
- for (const auto& elem : chain) {
- if (elem.first == key) {
- cout << elem.second << '\n';
- operation_complete = true;
- break;
- }
- }
- if (!operation_complete) {
- cout << -1 << '\n';
- }
- continue;
- }
- if (command == "delete") {
- auto slow = chain.begin();
- auto fast = chain.begin();
- while (fast != chain.end() && fast->first != key) {
- slow = fast;
- fast++;
- }
- if (fast == chain.end()) {
- cout << "error" << '\n';
- continue;
- }
- if (fast != slow) {
- chain.erase_after(slow);
- } else {
- chain.pop_front();
- }
- cout << "ok" << '\n';
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement