#训练2# 4-家谱处理

题目

https://pintia.cn/problem-sets/988034414048743424/problems/988040973986308096
在这里插入图片描述

思路

并查集,主要是STL容器的合理使用
①处理输入的空格
②如何查找存储字符串,并转换

代码

#include <iostream>
#include <cstring>
#include <stack>
#include <algorithm>
#include <queue>
#include <map>
#define MAXX 101
using namespace std;

int main()
{
    int n,m;
    cin>>n>>m;
    getchar();
    string s,s1;
    vector<string> v[MAXX];
    map<string ,string> fs;
    for (int i = 0; i < n; ++i)
    {
        getline(cin,s);
        int len = s.size();
        int cnt = 0;
        s1.clear();
        for (int j = 0; j < len; ++j)
        {
            if (s[j]==' ')
                cnt++;
            else
                s1+=s[j];
        }
       // cout<<s1<<endl;
        if (cnt == 0)
        {
            v[cnt].push_back(s1);
            fs[s1] = "0";
        }
        else
        {
            fs[s1] = v[cnt/2-1].back();
            v[cnt/2].push_back(s1);
        }
    }

    while (m--)
    {
        string  a,b,c,d,e,f;
        cin>>a>>b>>c>>d>>e>>f;
        //cout<<fs[a]<<endl;
        //cout<<f<<endl;
        if (d[0] == 'c')
        {
            if (fs[a] == f)
                cout<<"True"<<endl;
            else
                cout<<"False"<<endl;
        }
        else if (d[0] == 'p')
        {
            if (fs[f] == a)
                cout<<"True"<<endl;
            else
                cout<<"False"<<endl;
        }
        else if (d[0]=='s')
        {
            if (fs[a] == fs[f])
                cout<<"True"<<endl;
            else
                cout<<"False"<<endl;
        }
        else if (d[0] == 'd')
        {
            while (fs[a] != "0" && fs[a]!=f)
                a = fs[a];
            if (fs[a] == "0")
                cout<<"False"<<endl;
            else
                cout<<"True"<<endl;
        }
        else
        {
            while (fs[f] != "0" && fs[f]!=a)
                f = fs[f];
            if (fs[f] == "0")
                cout<<"False"<<endl;
            else
                cout<<"True"<<endl;
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值