题解 | #魔咒词典#
魔咒词典
https://ac.nowcoder.com/practice/c6ca566fa3984fae916e6d7beae8ea7f
#include <cstddef>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <bits/stdc++.h>
using namespace std;
int main() {
string s1;
map<string, string> m;
while (true) {
getline(cin, s1);
// getling会读取换行符(回车)之前的字符,并自动丢弃换行符
if (s1=="@END@") {
break;
}
string word = s1.substr(0, s1.find(']') + 1);
string info = s1.substr(s1.find(']') + 2);
// cout<<word<<"="<<info<<endl;
m[word] = info;
m[info] = word;
//看别人解析的妙用,不用再建一个map了
}
int n;
cin >> n;//cin不读取换行符而且不丢弃,故需要getchar()手动处理换行符
getchar();
while (n--) {
getline(cin, s1);
// getling会读取换行符(回车)之前的字符,并自动丢弃换行符
if (m.find(s1) != m.end()) {
string s2=m[s1];
if (s1.find(']')==string::npos) {//找到了
//s1中没有'[]',则结果m[s1]中有'[]',需要去除
s2.erase(s2.find('['));//erase(pos),pos只能是find的返回值
s2.erase(s2.find(']'));
cout<<s2<<endl;
} else
cout << s2 << endl;
} else {
cout << "what?" << endl;
}
}
}
// 64 位输出请用 printf("%lld")
#考研复试#