题目1035:找出直系亲属

时间限制:1 秒

内存限制:32 兆

特殊判题:否

提交:2931

解决:1145

题目描述:
如果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
-

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;public class Main {public static void main(String[] args) {new Task().solve() ;    }
}class Task{InputReader in = new InputReader(System.in) ;PrintWriter out = new PrintWriter(System.out) ;class Node{Node father , mother ;char val ;Node(char val , Node father , Node mother){this.val = val ;this.father = father ;this.mother = mother ;}}int dfs(Node first , Node second){if(first == second) return 0 ;if(first.father != null){int high = dfs(first.father , second) ;if(high != -1) return high + 1 ;}if(first.mother != null){int high = dfs(first.mother , second) ;if(high != -1) return high + 1 ;}   return -1 ;}void solve(){int n , m ;while(true){n = in.nextInt() ;m = in.nextInt() ;if(n == 0 && m == 0) break ; Map<Character , Node> mapper = new HashMap<Character, Task.Node>() ;while(n-- > 0){String args = in.next() ;Node father = mapper.get(args.charAt(1)) ;if(father == null){father = new Node(args.charAt(1) , null , null) ;mapper.put(args.charAt(1), father) ;}Node mother = mapper.get(args.charAt(2)) ;if(mother == null){mother = new Node(args.charAt(2) , null , null) ;mapper.put(args.charAt(2), mother) ;}Node node = mapper.get(args.charAt(0)) ;if(node == null){node = new Node(args.charAt(0) , father, mother) ;mapper.put(args.charAt(0), node) ;}else{node.father = father ;node.mother = mother ;}}while(m-- > 0){String args = in.next() ;if(args.charAt(0) == args.charAt(1)){out.println("-") ;continue ;}Node first = mapper.get(args.charAt(0)) ;Node second = mapper.get(args.charAt(1)) ;if(first == null || second == null){out.println("-") ;continue ;}int cnt = dfs(first, second) ;if(cnt != -1){if(cnt == 1) out.println("child") ;else if(cnt == 2) out.println("grandchild") ;else{for(int i = 1 ; i <= cnt - 2 ; i++) out.print("great-") ;out.println("grandchild") ;}}else{cnt = dfs(second , first) ;if(cnt != -1){if(cnt == 1) out.println("parent") ;else if(cnt == 2) out.println("grandparent") ;else{for(int i = 1 ; i <= cnt - 2 ; i++) out.print("great-") ;out.println("grandparent") ;}}else out.println("-") ;}}}out.flush();}
}class InputReader {  public BufferedReader reader;  public StringTokenizer tokenizer;  public InputReader(InputStream stream) {  reader = new BufferedReader(new InputStreamReader(stream), 32768);  tokenizer = new StringTokenizer("");  }  private void eat(String s) {  tokenizer = new StringTokenizer(s);  }  public String nextLine() {  try {  return reader.readLine();  } catch (Exception e) {  return null;  }  }  public boolean hasNext() {  while (!tokenizer.hasMoreTokens()) {  String s = nextLine();  if (s == null)  return false;  eat(s);  }  return true;  }  public String next() {  hasNext();  return tokenizer.nextToken();  }  public int nextInt() {  return Integer.parseInt(next());  }  public long nextLong() {  return Long.parseLong(next());  }  public double nextDouble() {  return Double.parseDouble(next());  }  public BigInteger nextBigInteger() {  return new BigInteger(next());  }  }

题目1035:找出直系亲属 树相关推荐

  1. 九度[1035]-找出直系亲属

    九度[1035]-找出直系亲属 题目描述: 如果A,B是C的父母亲,则A,B是C的parent,C是A,B的child,如果A,B是C的(外)祖父,祖母,则A,B是C的grandparent,C是A, ...

  2. 九度OJ 1035:找出直系亲属(二叉树)

    题目1035:找出直系亲属 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1309 解决:521 题目描述: 如果A,B是C的父母亲,则A,B是C的parent,C是A,B的child,如 ...

  3. hdu3786 找出直系亲属 水题

    题意: 找出直系亲属 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  4. HDU3786 找出直系亲属【关系闭包】

    找出直系亲属 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  5. KY122 找出直系亲属

    KY122 找出直系亲属 def cntGen(parentsDict, descendant, ascendant):if descendant not in parentsDict:return ...

  6. 九度1035:找出直系亲属并查集

    题目描述: 如果A,B是C的父母亲,则A,B是C的parent,C是A,B的child,如果A,B是C的(外)祖父,祖母,则A,B是C的grandparent,C是A,B的grandchild,如果A ...

  7. HDU_3786 找出直系亲属- softbar

    Problem Description 如果A,B是C的父母亲,则A,B是C的parent,C是A,B的child,如果A,B是C的(外)祖父,祖母,则A,B是C的grandparent,C是A,B的 ...

  8. 华为od机考题目-幼儿园找出同班的小朋友(幼儿园分班)

    幼儿园两个半的小朋哟排队的时候混合在一起了,每位小朋友都直到自己是否与前面的一位小朋友同伴,请你帮忙把同班的小朋友找出来 小朋友的编号是整数,与前一位同班用Y表示,不同班用N表示 准备两个列表分别保存 ...

  9. LeetCode 1379. 找出克隆二叉树中的相同节点(二叉树遍历)

    1. 题目 给你两棵二叉树,原始树 original 和克隆树 cloned,以及一个位于原始树 original 中的目标节点 target. 其中,克隆树 cloned 是原始树 original ...

  10. LeeCode 1379. 找出克隆二叉树中的相同节点

    文章目录 题目 题解 代码 题目 给你两棵二叉树,原始树 original 和克隆树 cloned,以及一个位于原始树 original 中的目标节点 target. 其中,克隆树 cloned 是原 ...

最新文章

  1. linux mysql解锁账号密码忘了怎么办,linux下mysql忘记密码怎么办
  2. VC socket api使用引入
  3. 红米note5解锁教程_红米NOTE5如何一键解锁?刷机教程图解
  4. TensorFlow官方入门实操课程-一个神经元的网络(线性曲线预测)
  5. 高等数学上-赵立军-北京大学出版社-题解-练习5.7
  6. Spring和JSF集成:MVC螺母和螺栓
  7. [多项式算法]多项式求逆 学习笔记
  8. 如何得知mysql表结构发生变化了呢?
  9. 前端开发工程师---技术路线图
  10. 一起来玩树莓派--解决官方docker源安装失败的问题
  11. 康奈尔大学研究员发现“代码投毒”攻击,可触发供应链攻击
  12. mybatis 原理_了解Mybatis的工作原理吗
  13. 计算机财务管理期末考试题及答案,计算机财务管理期末考工资部分题目及答案...
  14. 浅谈一下位操作符吧---我带你深入了解计算机内的数字(暂时限定整数)
  15. Win10设置屏保时间
  16. 【汉字识别】基于贝叶斯网络实现汉字识别含Matlab源码
  17. Intellij IDEA如何设置为中文界面?
  18. break,returned,和continue
  19. macOS微信客户端插件,支持免认证登录、多账号登录以及防撤回
  20. atx860和java_JAVA 版 ATX-Client

热门文章

  1. linux sub指令,数据处理指令之:SUB减操作指令
  2. c语言sub函数是什么,用$Super$$和$Sub$$对函数进行重定义
  3. .net MVC 局部视图
  4. 小白通过JDBC在AndroidStudio一步步来访问MYSQL数据库
  5. Chapter 8 Part-of-Speech Tagging
  6. 安庆集团-冲刺日志(第七天)
  7. MOT任务中JDE(Jointly learns the Detector and Embedding model)算法解读
  8. Vmware Workstation 无法连接到虚拟机,请确保您有权运行该程序。
  9. mysql mgr 启动_使用MySQL Shell创建MGR
  10. VUE输入 身份证 号,获取出生年月日