hdu 3786 寻找直系亲属

题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=3786

题目思路: 我们用p[i]=j 来表示 i 的孩子是j (因为一个parent只有一个孩子,但是一个孩子有两个parent不好形成对应)

然后初始化为-1,这样如果遇到父母不清楚的(‘-’) 还是-1,一遇到就break掉,把count还原为0;  如果是家族树上存在的两个结点,那么一定可以通过这样知道他们相隔的代数,于是就可以知道相应的称谓  一堆if-else  比较考基本功

代码:

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;

int p[26];
/*
int chartoint(char  ch)
{
    if(ch=='0')  return 0;
    else if(ch=='1')  return 1;
    else if(ch=='2')  return 2;
    else if(ch=='3')  return 3;
    else if(ch=='4')  return 4;
    else if(ch=='5')  return 5;
    else if(ch=='6')  return 6;
    else if(ch=='7')  return 7;
    else if(ch=='8')  return 8;
    else if(ch=='9')  return 9;
    else if(ch=='-')  return -1;

}
*/
int main()
{
   int n,m;
   while(cin>>n>>m)
   {
       if(n==0&&m==0)  break;
       memset(p,-1,sizeof(p));
       char c;
       char p1;
       char p2;

       string s;
       for(int i=0;i<n;i++)
       {
          cin>>s;
          c=s[0];
          p1=s[1];
          p2=s[2];

         if(p1!='-') p[p1-'A']=c-'A';
         if(p2!='-') p[p2-'A']=c-'A';

       }

       char c1,c2;
       for(int i=0;i<m;i++)
       {
          cin>>s;
          c1=s[0];
          c2=s[1];

          int count1=0,count2=0;
          int target1=c1-'A';
          int target2=c2-'A';
          int cur=target1;
          while(cur!=target2)
          {
             cur=p[cur];
             if(cur==-1)
             {
                 count1=0;
                 break;
             }

             else  count1++;

          }

          if(count1==0)
          {
             cur=target2;
              while(cur!=target1)
          {
             cur=p[cur];
             if(cur==-1)
             {
                 count2=0;
                 break;
             }
             else count2++;

          }


          }

         if(count1==0&&count2==0)
        {
           cout<<'-'<<endl;

        }

        else if(count1!=0)
        {
            if(count1==1)  cout<<"parent"<<endl;
            else if(count1==2)  cout<<"grandparent"<<endl;
            else
            {
               while(count1>=3)
               {
                   cout<<"great-";
                   count1--;
               }

                 cout<<"grandparent"<<endl;

            }
        }

        else if(count2!=0)
        {
            if(count2==1)  cout<<"child"<<endl;
            else if(count2==2)  cout<<"grandchild"<<endl;
            else
            {
               while(count2>=3)
               {
                   cout<<"great-";
                   count2--;
               }

                 cout<<"grandchild"<<endl;

            }
        }
       }
   }
}




转载于:https://www.cnblogs.com/814jingqi/p/3310426.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值