Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- struct Hashmap{
- vector<vector<pair<int, int>>> element;
- long capacity{};
- long hash(long key) const{
- return abs(key) % capacity;
- }
- void init(int size){
- this->element.resize(size);
- capacity = size;
- }
- void put(long key, long value){
- long hashed = hash(key);
- if(!this->element[hashed].empty() && this->element[hashed][0].first == key){
- this->element[hashed][0].second = value;
- }else{
- this->element[hashed].emplace_back(key, value);
- }
- }
- long get(long key) const{
- long hashed = hash(key);
- if(this->element[hashed].empty()){
- return -1;
- }else if(this->element[hashed][0].first == key){
- return this->element[hashed][0].second;
- }else{
- for(auto el : element[hashed]){
- if(el.first == key) return el.second;
- }
- return -1;
- }
- }
- long delete_pair(int key){
- long hashed = hash(key);
- if(this->element[hashed].empty()){
- return -1;
- }
- for(int i = 0; i < element[hashed].size(); i++){
- if(element[hashed][i].first == key){
- int val = element[hashed][i].second;
- this->element[hashed].erase(element[hashed].begin() + i);
- return val;
- }
- }
- }
- };
- int main(){
- ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
- Hashmap hashmap;
- int n;
- cin >> n;
- hashmap.init(n + 1);
- vector<string> res;
- while(n--){
- string request;
- cin >> request;
- if(request == "get" || request == "delete"){
- int key;
- cin >> key;
- int ans;
- if(request == "get"){
- ans = hashmap.get(key);
- }else{
- ans = hashmap.delete_pair(key);
- }
- if(ans == -1){
- res.emplace_back("None");
- }else{
- res.push_back(to_string(ans));
- }
- }else if(request == "put"){
- int key;
- int value;
- cin >> key >> value;
- hashmap.put(key, value);
- }
- }
- for(const auto& el : res){
- cout << el << '\n';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement