时间限制:1 秒

内存限制:32 兆

特殊判题:否

提交:2380

解决:934

题目描述:
    如果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
CDE
EFG
FA
BE
0 0
样例输出:
great-grandparent
-
来源:
2009年浙江大学计算机及软件工程研究生机试真题

思路:

因为本题的数据特点,将大写字母映射到数字,再利用数组建立二叉树比较方便。然后递归查找即可。

代码:

#include <stdio.h>

#define N 26

int a[N+1][2];

void init()
{
for (int i=0; i<=N; i++)
a[i][0] = a[i][1] = N;
} int c2i(char c)
{
return c-'A';
} int search(int x, int y, int step)
{
int k;
//printf("x=%d, y=%d, step=%d\n", x, y, step);
if (x == y)
return step;
if (a[x][0] == N && a[x][1] == N)
return -1;
if (a[x][0] != N)
{
k = search(a[x][0], y, step+1);
if (k >= 0)
return k;
}
if (a[x][1] != N)
{
k = search(a[x][1], y, step+1);
if (k >= 0)
return k;
}
return -1;
} void print(int step)
{
if (step > 0)
{
while (step > 2)
{
printf("great-");
step --;
}
if (step == 2)
printf("grand");
printf("child\n");
}
if (step < 0)
{
step = -step;
while (step > 2)
{
printf("great-");
step --;
}
if (step == 2)
printf("grand");
printf("parent\n");
}
} int main(void)
{
int n, m, i, x, y;
char s[10]; while (scanf("%d%d", &n, &m) != EOF)
{
if (n == 0 && m == 0)
break; init();
for (i=0; i<n; i++)
{
scanf("%s", s);
x = c2i(s[0]);
if (s[1] != '-')
a[x][0] = c2i(s[1]);
a[x][1] = c2i(s[2]);
} for (i=0; i<m; i++)
{
scanf("%s", s);
x = c2i(s[0]);
y = c2i(s[1]);
int step;
step = search(x, y, 0);
//printf("step = %d\n", step);
if (step > 0)
{
print(step);
continue;
}
step = search(y, x, 0);
//printf("step = %d\n", step);
if (step > 0)
{
print(-step);
continue;
}
printf("-\n");
}
} return 0;
}
/**************************************************************
Problem: 1035
User: liangrx06
Language: C
Result: Accepted
Time:0 ms
Memory:912 kb
****************************************************************/

最新文章

  1. 十天精通CSS3学习笔记 part1
  2. 烂泥:php5.6源码安装及php-fpm配置与nginx集成
  3. Linux socket多进程服务器框架二
  4. The default for KeyValuePair
  5. 自定义控件开发的调试及DesignMode的状态处理
  6. Codeforces Round #279 (Div. 2) C. Hacking Cypher 前缀+后缀
  7. 利用SecondaryNameNode文件恢复Namenode-实践可行
  8. 二分---LIGHTOJ 1062
  9. android:Faild to install,你的主机中的软件终止了一个连接错误解决
  10. 让你的代码量减少3倍!使用kotlin开发Android(二) --秘笈!扩展函数
  11. 排错-升级Exchange 2013 CU22后程序名称显示异常
  12. Java成员变量与局部变量的区别
  13. P2016 战略游戏
  14. 自己定义View Layout过程 - 最易懂的自己定义View原理系列(3)
  15. java连接https时禁用证书验证.
  16. 使用 Python 进行 socket 编程
  17. ASP.NET Web Pages:页面布局
  18. Beta阶段——第五篇 Scrum 冲刺博客
  19. 日记整理----&gt;2016-11-22
  20. iOS load和initialize的区别

热门文章

  1. Codeforces 777E Hanoi Factory(线段树维护DP)
  2. WPF 自动验证
  3. CSS属性clip
  4. 如何破解linux用户帐号密码二
  5. java模拟http的Get/Post请求,并设置ip与port代理
  6. JAVA Eclipse开发Android如何设置滚动条最大值最小值
  7. quartz 应用到 spring定时任务 执行两次
  8. Vue 中computed 与 methods 区别
  9. php excel文件导出之二 图像导出
  10. WinForm搭载ScintillaNET时文本由于发生偏移被隐藏解决方案