map可以实现多变量访问,string, for 循环

本文介绍如何在C++中利用map数据结构进行多变量访问,特别是结合for循环实现高效的操作。通过实例解析map的用法,重点讨论了在蓝桥杯竞赛中的应用,以及与散列表的对比。
摘要由CSDN通过智能技术生成
/*
* map可以嵌套结构体实现多变量访问
* for 循环里可以套string
* 对于题目中的字符串以空格隔开
* 可以思考为什么会这样子
* 这样肯定是存储更简便
* substr(开始位置, 结束位置, 要新建字符串的源字符串)
* 
*/

#include <bits/stdc++.h>

using namespace std;
const int N = 1E5 + 10;
int n, m;
string a, b, c, d;
struct node
{
	char sex;
	string father;
};
map<string, node> people;

int judge(string a, string b)
{
	int i = 1, j;
	for (string A = a; !A.empty(); A = people[A].father, ++i)
	{
		int j = 1;
		for (string B = b; !B.empty(); B = people[B].father, ++j)
		{
			if (i >= 5 && j >= 5)
				return 1;
			if (A == B)
				return 0;
		}
	}
	return 1;
}

int main()
{
	cin >> n; 
	getchar();
	for (int i = 1; i <= n; ++i)
	{
		cin >> a >> b;
		if (b[b.length() - 1] == 'n')
		{
			people[a] = { 'm', b.substr(0, b.length() - 4) };
		}
		else if (b[b.length() - 1] == 'r')
		{
			people[a] = { 'f', b.substr(0, b.length() - 7) };
		}
		else
		{
			people[a].sex = b[b.length() - 1];
		}
	}
	cin >> m;
	getchar();
	for (int i = 1; i <= m; ++i)
	{
		cin >> a >> b >> c >> d;
		if (people.count(a) == 0 || people.count(c) == 0)
		{
			cout << "NA" << endl;
		}
		else if (people[a].sex == people[c].sex)
		{
			cout << "Whatever" << endl;
		}
		else
		{
			judge(a, c) ? puts("Yes") : puts("No") ;
		}
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值