online_judge_1035

183 篇文章 0 订阅
#include <iostream>

using namespace std;

int Tree[30];
int main()
{
    char s1[4];
    int n,m;
    int i;
    int level;
    int flag;
    int index1,index2,index3;
    while(cin>>n>>m)
    {
        if(m==0 && n==0)
            break;
        for(i=0; i<30; ++i)
            Tree[i] = -1;
        for(i=0; i<n; ++i)
        {
            cin>>s1;
            index1 = s1[0] - 'A';
            index2 = s1[1] - 'A';
            index3 = s1[2] - 'A';
            Tree[index2] = index1;
            Tree[index3] = index1;
            //Tree[index1] = -2;
        }
        for(i=0; i<m; ++i)
        {
            cin>>s1;
            index1 = s1[0] - 'A';
            index2 = s1[1] - 'A';
            flag = 0;

            index3 = index1;
            level = 0;
            while(Tree[index3] >= 0)
            {
                level++;
                index3 = Tree[index3];
                if(index3 == index2)
                {
                    flag = 1;
                    break;
                }
            }
            if(flag != 1) //这一步十分重要,细节决定成败……
            {
                index3 = index2;
                level = 0;
            }
            while(flag!=1 && Tree[index3] >= 0)
            {
                level++;
                index3 = Tree[index3];
                if(index3 == index1)
                {
                    flag = 2;
                    break;
                }
            }
            if(flag == 0)
            {
                cout<<"-"<<endl;
            }
            else if(flag == 1)
            {
                while(level>2)
                {
                    cout<<"great-";
                    level--;
                }
                if(level == 2)
                    cout<<"grandparent"<<endl;
                if(level == 1)
                    cout<<"parent"<<endl;
            }
            else if(flag == 2)
            {
                while(level>2)
                {
                    cout<<"great-";
                    level--;
                }
                if(level == 2)
                    cout<<"grandchild"<<endl;
                if(level == 1)
                    cout<<"child"<<endl;
            }
        }
    }
    return 0;
}


这道题目有点意思,有点含金量,对hash的考察很到位

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值