冰岛人[天梯赛]

题目描述

在这里插入图片描述
在这里插入图片描述

输入样例
15
chris smithm
adam smithm
bob adamsson
jack chrissson
bill chrissson
mike jacksson
steve billsson
tim mikesson
april mikesdottir
eric stevesson
tracy timsdottir
james ericsson
patrick jacksson
robin patricksson
will robinsson
6
tracy tim james eric
will robin tracy tim
april mike steve bill
bob adam eric steve
tracy tim tracy tim
x man april mikes


输出样例
Yes
No
No
Whatever
Whatever
NA

思路

模拟

存储结构
1.结构体存储每个人的信息,包括父亲的姓名(只针对维京人后裔)以及每个人的性别
2.用map存储每个人姓名到其结构体信息的映射

具体流程
1.判断两人是否都存在 – 一定要首先判断
2.判断两人性别是否相同
3.判断两人是否五代内有公共祖先
①依次遍历每个人的祖先,判断五代内是否有公共祖先,如果超出五代,或者祖先不足5代(不管相同还是不用),则可以交往;否则不可以交往

AC代码

#include <bits/stdc++.h>
using namespace std;
typedef struct
{
    char sex;
    string family_name;
}person;
map<string, person> mp;
bool judge(string a, string b)
{
    int cnta = 1; //统计A祖先的代数
    for(string A = a; A.size(); A = mp[A].family_name)
    {
        int cntb = 1; //统计B祖先的代数
        for(string B = b; B.size(); B = mp[B].family_name)
        {
            if(cnta >= 5 && cntb >= 5) return true; //五代之内没有祖先
            if(A == B && (cnta < 5 || cntb < 5)) return false; //五代之内有相同祖先
            cntb ++;        
        }
        cnta ++;
    }
    return true; //不够五代
}
int main()
{
    int n;
    cin >> n;
    for(int i = 0; i < n; i ++)
    {
        string s1, s2;
        cin >> s1 >> s2;
        if(s2.find("sson") != -1) //维京人后裔 男性
        {
            int pos = s2.find("sson");
            mp[s1] = {'m', s2.substr(0, pos)};
        }
        else if(s2.find("sdottir") != -1) //维京人后裔 女性
        {
            int pos = s2.find("sdottir");
            mp[s1] = {'f', s2.substr(0, pos)};
        }
        else
        {
            int len = s2.size();
            if(s2[len - 1] == 'm') mp[s1].sex = 'm';
            else if(s2[len - 1] == 'f') mp[s1].sex = 'f';
        }
    }
    int m;
    cin >> m;
    while(m --)
    {
        string name1, family_name1, name2, family_name2;
        cin >> name1 >> family_name1 >> name2 >> family_name2;
        if(mp.find(name1) == mp.end() || mp.find(name2) == mp.end()) cout << "NA" << endl;
        else if(mp[name1].sex == mp[name2].sex) cout << "Whatever" << endl;
        else
        {
            if(judge(name1, name2)) cout << "Yes" << endl;
            else cout << "No" << endl;
        }
    }
    return 0;
}

欢迎大家批评指正!!!

根据提供的引用内容,没有找到关于冰岛国旗的Python脚本或相关信息。但是,我可以为您提供一些关于如何在Python中绘制冰岛国旗的示例代码。 ```python import turtle # 设置画布大小 turtle.setup(600, 400) # 创建画笔 pen = turtle.Turtle() # 绘制蓝色背景 pen.penup() pen.goto(-300, 200) pen.pendown() pen.color("blue") pen.begin_fill() for _ in range(2): pen.forward(600) pen.right(90) pen.forward(200) pen.right(90) pen.end_fill() # 绘制红色横线 pen.penup() pen.goto(-300, 100) pen.pendown() pen.color("red") pen.begin_fill() pen.forward(600) pen.right(90) pen.forward(40) pen.right(90) pen.forward(600) pen.right(90) pen.forward(40) pen.end_fill() # 绘制白色横线 pen.penup() pen.goto(-300, 60) pen.pendown() pen.color("white") pen.begin_fill() pen.forward(600) pen.right(90) pen.forward(40) pen.right(90) pen.forward(600) pen.right(90) pen.forward(40) pen.end_fill() # 绘制红色横线 pen.penup() pen.goto(-300, 20) pen.pendown() pen.color("red") pen.begin_fill() pen.forward(600) pen.right(90) pen.forward(40) pen.right(90) pen.forward(600) pen.right(90) pen.forward(40) pen.end_fill() # 绘制白色横线 pen.penup() pen.goto(-300, -20) pen.pendown() pen.color("white") pen.begin_fill() pen.forward(600) pen.right(90) pen.forward(40) pen.right(90) pen.forward(600) pen.right(90) pen.forward(40) pen.end_fill() # 绘制红色横线 pen.penup() pen.goto(-300, -60) pen.pendown() pen.color("red") pen.begin_fill() pen.forward(600) pen.right(90) pen.forward(40) pen.right(90) pen.forward(600) pen.right(90) pen.forward(40) pen.end_fill() # 绘制红色竖线 pen.penup() pen.goto(-260, 200) pen.pendown() pen.color("red") pen.begin_fill() pen.forward(40) pen.right(90) pen.forward(200) pen.right(90) pen.forward(40) pen.right(90) pen.forward(200) pen.end_fill() # 绘制白色竖线 pen.penup() pen.goto(-220, 200) pen.pendown() pen.color("white") pen.begin_fill() pen.forward(40) pen.right(90) pen.forward(200) pen.right(90) pen.forward(40) pen.right(90) pen.forward(200) pen.end_fill() # 绘制红色竖线 pen.penup() pen.goto(-180, 200) pen.pendown() pen.color("red") pen.begin_fill() pen.forward(40) pen.right(90) pen.forward(200) pen.right(90) pen.forward(40) pen.right(90) pen.forward(200) pen.end_fill() # 隐藏画笔 pen.hideturtle() # 显示绘图窗口 turtle.done() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值