题解 | #魔咒词典#
魔咒词典
https://www.nowcoder.com/practice/c6ca566fa3984fae916e6d7beae8ea7f
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
// 写算法题,对于字符串的输出要与正确结果比对清楚
int main() {
// cin>>n ;
string s ;
map<string ,string> mp ;
// getline 会自动忽略换行。
while(getline(cin,s)) // 获得一行的字符串。
{
if(s == "@END@")
{
break;
}
int pos = s.find(']') ;
string mozhou = s.substr(0,pos+1-0 ) ; // 找到魔咒。
string tool = s.substr(pos+2) ;
// cout<<tool <<tool.size()<<endl ;
mp[mozhou] =tool ;
mp[tool] = mozhou ;
}
int n ;
// cin>> n ; // n个数。
scanf("%d",&n) ;
getchar() ;
string requre ;
while(n--)
{
getline(cin, requre); //
if(requre[0] == '[') // 魔咒。
{
// cout<<"requre"<<requre<<endl ;
if(mp.find(requre) != mp.end()) // 如果找到了
{
cout<<mp[requre]<<endl ;
}
else {
cout<<"what?"<<endl ;
}
}else {// 输入的是功能,需要找到对应的咒语。
// cout<<"requre "<<requre<<endl ;
if(mp.find(requre)!= mp.end()) // 如果找到了功能
{
string re = mp[requre] ;
// cout<<"re "<<re<<endl ;
string res = re.substr(1 ,re.size() -2 ) ;
cout<<res<<endl ;
}else{
cout<<"what?"<<endl ;
}
}
// string res = mp[requre];
// if(res[0] == '[')
// {
// cout<<res.substr(1 , res.size()-2) <<endl ;
// continue;
// }else if(res == "")
// {
// cout<<"what?"<<endl ;
// continue ;
// }else {
// cout<<res <<endl ;
// }
}
}
// 64 位输出请用 printf("%lld")
查看7道真题和解析
海康威视公司福利 1235人发布