1543 - 找出直系亲属

通过次数

0

提交次数

0

时间限制 : 1 秒 内存限制 : 32 MB

 如果A,B是C的父母亲,则A,B是C的parent,C是A,B的child,如果A,B是C的(外)祖父,祖母,则A,B是C的grandparent,C是A,B的grandchild,如果A,B是C的(外)曾祖父,曾祖母,则A,B是C的great-grandparent,C是A,B的great-grandchild,之后再多一辈,则在关系上加一个great-。

题目输入

输入包含多组测试用例,每组用例首先包含2个整数n(0<=n<=26)和m(0<m<50), 分别表示有n个亲属关系和m个问题, 然后接下来是n行的形式如ABC的字符串,表示A的父母亲分别是B和C,如果A的父母亲信息不全,则用-代替,例如A-C,再然后是m行形式如FA的字符串,表示询问F和A的关系。

当n和m为0时结束输入。

题目输出

如果询问的2个人是直系亲属,请按题目描述输出2者的关系,如果没有直系关系,请输出-。

具体含义和输出格式参见样例.

输入/输出样例

输入格式

3 2
ABC
EFG
CDE
EB
AF
0 0

输出格式

-
great-grandchild

C语言解答

#include <stdio.h>
int main()
{
  while(1)
  {
    int n,m;
    scanf("%d%d",&n,&m);
    if(n==0&&m==0)
    {
      break;
    }
    int notes[26];
    for(int i=0;i<26;i++)
    {
      notes[i]=-1;
    }
    for(int i=0;i<n;i++)
    {
      char ch[4];
      scanf("%s",ch);
      if(ch[1]!='-')
      {
        notes[ch[1]-'A']=ch[0]-'A';
      }
      if(ch[2]!='-')
      {
        notes[ch[2]-'A']=ch[0]-'A';
      }
    }
    for(int i=0;i<m;i++)
    {
      char ch[4];
      scanf("%s",ch);
      int count=-1;
      int j=ch[0]-'A';
      while(notes[j]!=-1&&notes[j]!=ch[1]-'A')
      {
        count++;
        j=notes[j];
      }
      if(notes[j]!=-1)
      {
        for(int k=0;k<count;k++)
        {
          printf("great-");
        }
        if(count!=-1)
        {
          printf("grandparent\n");
        }
        else
        {
          printf("parent\n");
        }
        continue;
      }
      count=-1;
      j=ch[1]-'A';
      while(notes[j]!=-1&&notes[j]!=ch[0]-'A')
      {
        count++;
        j=notes[j];
      }
      if(notes[j]!=-1)
      {
        for(int k=0;k<count;k++)
        {
          printf("great-");
        }
        if(count!=-1)
        {
          printf("grandchild\n");
        }
        else
        {
          printf("child\n");
        }
        continue;
      }
      printf("-\n");
    }
  }
}

C++解答

#include <cstdio>
#include <cmath>
#include <map>
#include <algorithm>
using namespace std;

map < char, char > child;

int cal(char a, char b) {
    int ret = 0;
    while (a != child[a]) {
        if (a == b)
            return ret;
        a = child[a];
        ++ret;
    }
    return 0;
}

int main() {
    //freopen("data.in", "r", stdin);
    //freopen("data.out", "w", stdout);
    int n, m;
    while (EOF != scanf("%d %d", &n, &m)) {
        if (0 == n && 0 == m)
            break;
        child.clear();
        char s[10];
        while (n--) {
            scanf("%s", s);
            for (int i = 1; i < 3; ++i)
                if ('-' != s[i])
                    child[ s[i] ] = s[0];
        }
        while (m--) {
            scanf("%s", s);
            int dis = cal(s[0], s[1]) - cal(s[1], s[0]);
            if (0 == dis)
                puts("-");
            else {
                for (int i = abs(dis); i > 2; --i)
                    printf("great-");
                if (abs(dis) >= 2)
                    printf("grand");
                puts( dis > 0 ? "parent" : "child" );
            }
        }
    }
    return 0;
}