九度1029 -map - 魔咒词典

来源:互联网 发布:云计算架构师是干嘛的 编辑:程序博客网 时间:2024/05/16 09:24

这个题目就是为了练习map,因为有可能从key查找value,由value查找key两个情况,所以需要两个map,因为map只能从key查找value。同时也考验了字符串的处理。

#include<iostream>#include<map>#include<string>#include<cstdio>using namespace std; int main(){         map<string,string>maps;    map<string,string>maps1;    map<string,string>::iterator f;    string a;    int n;    while(getline(cin,a)){        if(a=="@END@")            break;        int end = a.find(']');        string tmp = a.substr(1,end-1);        string tmp1 = a.substr(end+2,a.length()-1);        maps[tmp]=tmp1;        maps1[tmp1]=tmp;    }    cin>>n;    getchar();    for(int i=0;i<n;i++){        getline(cin,a);        if(a.find('[')!=-1){            a = a.substr(1,a.length()-2);            f = maps.find(a);            if(f==maps.end())                cout<<"what?"<<endl;            else                cout<<maps[a]<<endl;        }        else{            f = maps1.find(a);            if(f==maps1.end())                cout<<"what?"<<endl;            else                cout<<maps1[a]<<endl;        }    }    return 0;}


0 0