题解 | #魔咒词典#

魔咒词典

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

//土尔逊Torson 编写于2023/06/13
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <string>
#include <map>

using namespace std;

map<string, string> dictionary;

int main() {
	string str;
	while (getline(cin, str)) {
		if (str == "@END@") {
			break;
		}
		int pos = str.find("]");             //分界点
		string key = str.substr(0, pos + 1); //魔咒
		string value = str.substr(pos + 2);  //功能
		dictionary[key] = value;
		dictionary[value] = key;
	}
	int n;
	scanf("%d", &n);
	getchar();                               //吃掉回车
	while (n--) {
		string key;
		getline(cin, key);
		string answer = dictionary[key];
		if (answer == "") {                  //魔咒或者功能找不到
			answer = "what?";
		}
		else if (answer[0] == '[') {         //魔咒需要删除方括号
			answer = answer.substr(1, answer.size() - 2);
		}
		cout << answer << endl;
	}
	system("pause");
	return EXIT_SUCCESS;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

09-18 20:41
门头沟学院 Java
要个offer怎么这...:哈哈哈哈哈哈,我也拿了0x10000000个offer,秋招温啦啦啦,好开心
我的秋招日记
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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