题解 | #魔咒词典#
魔咒词典
https://www.nowcoder.com/practice/c6ca566fa3984fae916e6d7beae8ea7f
map的三种类型中只有multimap是不支持下标访问和写入的,因为键值不唯一
#include <iostream>
#include "unordered_map"
using namespace std;
int main() {
string s;
unordered_map<string, string> myMap;
while (getline(cin, s)) { // 注意 while 处理多个 case
// cout << a + b << endl;
if (s != "@END@") {
int pos = s.find(']');
myMap[s.substr(0, pos + 1)] = s.substr(pos + 2);
myMap[s.substr(pos + 2)] = s.substr(0, pos + 1);
} else {
int n;
cin >> n;
getchar();
while (n--) {
string ask;
getline(cin, ask);
auto it = myMap.find(ask);
if (it == myMap.end()) cout << "what?" << endl;
else {
string ans = it->second;
if (ask[0] != '[') {
ans = ans.substr(1, ans.size() - 2);
}
cout << ans << endl;
}
}
}
}
}
// 64 位输出请用 printf("%lld")

