/*
* 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;
}
map可以实现多变量访问,string, for 循环
最新推荐文章于 2023-01-16 10:49:57 发布
本文介绍如何在C++中利用map数据结构进行多变量访问,特别是结合for循环实现高效的操作。通过实例解析map的用法,重点讨论了在蓝桥杯竞赛中的应用,以及与散列表的对比。
摘要由CSDN通过智能技术生成