table of Contents
topic
Portal: ITP2_8_A: the Map: the Delete
description
Dictionary for storing the M element formed by a key string and integer values, perform the operation given below, note that, in each of the M key must be unique
- \ (INSERT (Key, X) \) : Insert an element and a value of the key to the M
- (GET (Key) \) \ : key value corresponding to a specified output, if there is no element that outputs 0
- \ (the Delete (Key) \) : Delete the value corresponding to the specified key
Entry
Input formats are given in the following
\(q\)
\(query_{1}\)
\(query_{2}\)
\(.\)
\(.\)
\(query_{n}\)
Each \ (query_ {i} \) are:
\ (0 \) \ (Key \) \ (X \)
or
\ (. 1 \) \ (Key \)
or
\ (2 \) \ (Key \)
wherein the first number 0, 1 and 2 denote insert, get, delete operation.
Export
For each get operation, the output line of an integer
limitation factor
- \ (1 \ leq q \ leq 200000 \)
- \ (1 \ leq x \ leq 1000000000 \)
- \(1 \leq\) length of \(key \leq 20\)
- \ (key \) all lowercase letters
Sample input
7
0 blue 4
0 red 1
0 white 5
1 red
1 blue
0 black 8
1 black
Sample Output
1
4
0
0
Solving
To achieve their own
analysis
Classic exercises on the hash table, not much explanation
design
Ditto once the design process, but adds delete function
can be directly modified for this question, if com == 2, insert (key, 0); key corresponds directly modify the value 0 would be finished
but for the common and clear out space, will need to be removed elements key to a specific value, this particular value is considered to be an element present in the Find and delete, only added element is considered to be no elements, but note that when you add an element, it is necessary first determine there is no current corresponding to the key element, if not, can only be regarded as a specific value space. otherwise, the situation may appear two keys correspond to different values.
coding
#include <iostream>
#include <string>
using namespace std;
#define MAX_Q 400005
struct Ele {
string key;
int value;
Ele() {
key = "";
value = 0;
}
Ele(string str, int i) {
key = str;
value = i;
}
};
Ele space[MAX_Q];
long get_key(string str) {
long total = 0;
for (int i = 0; i < str.length(); i++) {
total = (total * 4 + str[i] - 'a' + 1) % 1000000007;
}
return total;
}
void insert(string key, int value) {
int index = get_key(key) % MAX_Q;
while (space[index].key != "" && space[index].key != key) {
index = (index + 17541) % MAX_Q;
}
// 如果有的话直接更新
if (space[index].key == key) {
space[index].value = value;
return;
}
// 没有的话再找空位进行插入
index = get_key(key) % MAX_Q;
while (space[index].key != "" && space[index].key != "{}") {
index = (index + 17541) % MAX_Q;
}
Ele m(key, value);
space[index] = m;
}
int search(string key) {
int index = get_key(key) % MAX_Q;
while (space[index].key != "" && space[index].key != key) {
index = (index + 17541) % MAX_Q;
}
if (space[index].key == key) {
return space[index].value;
} else {
return 0;
}
}
void remove(string key) {
int index = get_key(key) % MAX_Q;
while (space[index].key != "" && space[index].key != key) {
index = (index + 17541) % MAX_Q;
}
if (space[index].key == key) {
space[index].key = "{}";
}
}
int main(void) {
ios::sync_with_stdio(false);
cin.tie(0);
int q;
cin >> q;
int com, value;
string key;
while (q--) {
cin >> com >> key;
if (com == 0) {
cin >> value;
insert(key, value);
} else if (com == 1) {
cout << search(key) << endl;
} else {
remove(key);
}
}
}
result
to sum up
Amount, wrong again several times
for the first time: If the conflict back to a linear, two keys a, b are calculated position is 0, a 0 and placed in position, b into the No. 1 position, delete the a, at this position 0 to a specific value when adding b elements, due to the specific value is treated as empty elements are added directly into the 0 position, and when after deleting b element, just delete the element 0 position, get the time will display elements No. 1 position, resulting in an error when adding elements, first check whether there is, then there is no specific values as empty add a new element.
second: after checking whether there is over, forget the pointer finger back ...
STL
coding
#include <bits/stdc++.h>
using namespace std;
int main(void) {
ios::sync_with_stdio(false);
cin.tie(0);
int q, com, x;
string key;
map<string, int> my_map;
cin >> q;
while (q--) {
cin >> com >> key;
if (com == 0) {
cin >> x;
my_map[key] = x;
} else if(com == 1) {
cout << my_map[key] << endl;
} else {
my_map[key] = 0;
}
}
}
result
Remark
no