题解 | #魔咒词典#

魔咒词典

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")

全部评论

相关推荐

10-17 23:18
已编辑
西北农林科技大学 Web前端
独行m:给25可以试试,但他只能给12,那就是纯纯的事精
秋招,不懂就问
点赞 评论 收藏
分享
09-22 15:45
门头沟学院 Java
谁给娃offer我给...:我也遇到了,我说只要我通过面试我就去,实际上我根本就不会去😁
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务