九度oj 题目1035:找出直系亲属
- 题目描述:
-
如果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
-#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#define MIN -999999
int father[];
int mother[]; char temp[]; int relation[][]; int find(int p, int q) {
if(father[p] == q) {
return ;
}
if(mother[p] == q) {
return ;
}
if(relation[p][q] != MIN) {
return relation[p][q];
}
if(father[p] != -) {
int ansf = find(father[p],q);
if(ansf != ) {
relation[p][q] = + ansf;
return + ansf;
}
}
if(mother[p] != -) {
int ansm = find(mother[p],q);
if(ansm != ) {
relation[p][q] = + ansm;
return + ansm;
}
}
return ; } void prinfop(int n) {
if(n == ) {
puts("-");
}
else if(n == ) {
puts("parent");
}
else if(n == ) {
puts("grandparent");
}
else {
for(int i = ; i < n; i++) {
printf("%s","great-");
}
puts("grandparent");
}
} void prinfoc(int n) {
if(n == ) {
puts("-");
}
else if(n == ) {
puts("child");
}
else if(n == ) {
puts("grandchild");
}
else {
for(int i = ; i < n; i++) {
printf("%s","great-");
}
puts("grandchild");
}
}
int main(int argc, char const *argv[])
{
int n, m;
//freopen("input.txt","r",stdin);
scanf("%d %d",&n, &m);
while(!(n == && m == )) {
for(int i = ; i < ; i++) {
father[i] = -;
mother[i] = -;
for(int j = ; j < ; j++) {
relation[i][j] = MIN;
}
}
for(int i = ; i < n; i++) {
scanf("%s",temp);
if(temp[] == '-') {
continue;
}
int child = temp[] - 'A';
if(temp[] != '-') {
int fa = temp[] - 'A';
father[child] = fa;
relation[child][fa] = ;
}
if(temp[] != '-') {
int ma = temp[] - 'A';
mother[child] = ma;
relation[child][ma] = ;
} }
for(int i = ; i < m; i++) {
scanf("%s",temp);
if(temp[] == '-' || temp[] == '-') {
puts("-");
continue;
}
int p1 = temp[] - 'A';
int p2 = temp[] - 'A';
int ans1 = find(p1, p2);
if(ans1 == ) {
int ans2 = find(p2,p1);
prinfop(ans2);
}
else {
prinfoc(ans1);
}
}
scanf("%d %d",&n, &m); }
return ;
}这道题用了二叉树的遍历和动态规划的思想,最重要的是一遍通过了(忽略第一次忘记注释freopen),开心!
九度oj 题目1035:找出直系亲属的更多相关文章
- 九度OJ 1256:找出两个只出现了一次的数字 (位运算)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:568 解决:186 题目描述: 一个整型数组里除了两个数字之外,其他的数字都出现了两次.请写程序找出这两个只出现一次的数字. 输入: 输入的 ...
- 九度oj 1407 快速找出最小数
原题链接:http://ac.jobdu.com/problem.php?pid=1407 线段树,区间更新,查询区间最小值. 注意区间更新,查询的时候,区间$\begin{align*}[L,R] ...
- hdu 1284 关于钱币兑换的一系列问题 九度oj 题目1408:吃豆机器人
钱币兑换问题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- 九度oj题目&吉大考研11年机试题全解
九度oj题目(吉大考研11年机试题全解) 吉大考研机试2011年题目: 题目一(jobdu1105:字符串的反码). http://ac.jobdu.com/problem.php?pid=11 ...
- 九度oj 题目1007:奥运排序问题
九度oj 题目1007:奥运排序问题 恢复 题目描述: 按要求,给国家进行排名. 输入: 有多组数据. 第一行给出国家数N,要求排名的国家数M,国家号 ...
- 九度OJ 题目1384:二维数组中的查找
/********************************* * 日期:2013-10-11 * 作者:SJF0115 * 题号: 九度OJ 题目1384:二维数组中的查找 * 来源:http ...
- 九度OJ 1035:找出直系亲属(二叉树)
题目1035:找出直系亲属 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1309 解决:521 题目描述: 如果A,B是C的父母亲,则A,B是C的parent,C是A,B的child,如 ...
- 九度OJ题目1105:字符串的反码
tips:scanf,cin输入字符串遇到空格就停止,所以想输入一行字符并保留最后的"\0"还是用gets()函数比较好,九度OJ真操蛋,true?没有这个关键字,还是用1吧,还是 ...
- 九度oj题目1009:二叉搜索树
题目描述: 判断两序列是否为同一二叉搜索树序列 输入: 开始一个数n,(1<=n<=20) 表示有n个需要判断,n= 0 的时候输入结束. 接 ...
随机推荐
- mysql实现计数器
本文转自:https://blog.csdn.net/stevendbaguo/article/details/70889449 如果是在非常高的并发之下,还是建议用内存数据库redis去实现计数的功 ...
- HDU4576 Robot(概率)
题意 抄袭自https://www.cnblogs.com/Paul-Guderian/p/7624039.html 多组输入n,m,l,r.表示在一个环上有n个格子.接下来输入m个w表示连续的一段 ...
- Eclipse优化工具Optimizer for Eclipse
第一次看到是Optimizer for Eclipse是在InfoQ 然后使用了一下,发现不错啊,我的好几年的破本都能比较快的启动Eclipse了 好了,废话不说了,来介绍一下Optimizer fo ...
- Android adb命令,linux中各种命令
常用的ADB命令 1. 显示系统中全部Android平台: android list targets 2. 显示系统中全部AVD(模拟器): android list avd 3. 创建AVD(模拟器 ...
- sysdig安装和使用介绍
安装步骤1)安装资源库rpm --import https://s3.amazonaws.com/download.draios.com/DRAIOS-GPG-KEY.publiccurl -s -o ...
- 动手使用ABAP Channel开发一些小工具,提升日常工作效率
今天的故事要从ABAP小游戏说起. 中国的ABAP从业者们手头或多或少都搜集了一些ABAP小游戏,比如下面这些. 消灭星星: 扫雷: 来自我的朋友刘梦,公众号"SAP干货铺"里的俄 ...
- EF关于报错Self referencing loop detected with type的原因以及解决办法
1)具体报错 { "Message": "出现错误.", "ExceptionMessage": "“ObjectContent` ...
- shell脚本,按单词出现频率降序排序。
[root@localhost oldboy]# cat file the squid project provides a number of resources toassist users de ...
- Ruby设计模式-观察者模式学习笔记
observer.rb #!/bin/env ruby # encoding: utf-8 require 'observer' class CriminalMovement include Obse ...
- hihoCoder-1098-kruskal
如果起始点和终止点的父节点相同,就说明它们就已经在同一个连通分量里面,说明,起始点和终止点在此之前就已经被连入同一个分量之中,如果此时还将起始点和终止点连入此分量,就会形成回路,想象一个三角形,你大概 ...