题解 | #魔咒词典#

魔咒词典

http://www.nowcoder.com/practice/c6ca566fa3984fae916e6d7beae8ea7f

#include<iostream>
#include<map>
#include<string>
using namespace std;

int main(){
    string str, magicName, function;
    map<string, string>myMap1, myMap2;
    while(getline(cin,str)){
        if(str=="@END@") break;
        int pos = str.find("]");
        magicName = str.substr(1,pos-1);
        function = str.substr(pos+2);
        myMap1[magicName] = function;
        myMap2[function] = magicName;
    }
    int n;
    scanf("%d\n",&n);
    for(int i = 0; i < n; i++){
        getline(cin, str);
        if(str.find("]")!=string::npos){
            string O = myMap1[str.substr(1,str.size()-2)];
            if(O=="") cout<<"what?"<<endl;
            else cout<<myMap1[str.substr(1,str.size()-2)]<<endl;
        }else{
            string O = myMap2[str];
            if(O == "") cout<<"what?"<<endl;
            else cout<<myMap2[str]<<endl;
        }
    }
    return 0;
}

本题使用map来建立魔咒名称和功能之间的映射关系,然后根据后续的查询指令来进行查找输出即可。

全部评论

相关推荐

点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务