zoukankan      html  css  js  c++  java
  • 九度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
    -
      1 #include <cstdio>
      2 #include <cstdlib>
      3 #include <cstring>
      4 #include <string>
      5 #include <algorithm>
      6 #define MIN -999999
      7 int father[27];
      8 int mother[27];
      9 
     10 char temp[10];
     11 
     12 int relation[27][27];
     13 
     14 int find(int p, int q) {
     15     if(father[p] == q) {
     16         return 1;
     17     }
     18     if(mother[p] == q) {
     19         return 1;
     20     }
     21     if(relation[p][q] != MIN) {
     22         return relation[p][q];
     23     }
     24     if(father[p] != -1) {
     25         int ansf = find(father[p],q);
     26         if(ansf != 0) {
     27             relation[p][q] = 1 + ansf;
     28             return 1 + ansf;
     29         }   
     30     }
     31     if(mother[p] != -1) {
     32         int ansm = find(mother[p],q);
     33         if(ansm != 0) {
     34             relation[p][q] = 1 + ansm;
     35             return 1 + ansm;
     36         }
     37     }
     38     return 0;
     39     
     40 }
     41 
     42 void prinfop(int n) {
     43     if(n == 0) {
     44         puts("-");
     45     }
     46     else if(n == 1) {
     47         puts("parent");
     48     }
     49     else if(n == 2) {
     50         puts("grandparent");
     51     }
     52     else {
     53         for(int i = 2; i < n; i++) {
     54             printf("%s","great-");
     55         }
     56         puts("grandparent");
     57     }
     58 }
     59 
     60 void prinfoc(int n) {
     61     if(n == 0) {
     62         puts("-");
     63     }
     64     else if(n == 1) {
     65         puts("child");
     66     }
     67     else if(n == 2) {
     68         puts("grandchild");
     69     }
     70     else {
     71         for(int i = 2; i < n; i++) {
     72             printf("%s","great-");
     73         }
     74         puts("grandchild");
     75     }
     76 }
     77 int main(int argc, char const *argv[])
     78 {
     79     int n, m;
     80     //freopen("input.txt","r",stdin);
     81     scanf("%d %d",&n, &m);
     82     while(!(n == 0 && m == 0)) {
     83         for(int i = 0; i < 30; i++) {
     84             father[i] = -1;
     85             mother[i] = -1;
     86             for(int j = 0; j < 30; j++) {
     87                 relation[i][j] = MIN;
     88             }
     89         }
     90         for(int i = 0; i < n; i++) {
     91             scanf("%s",temp);
     92             if(temp[0] == '-') {
     93                 continue;
     94             }
     95             int child = temp[0] - 'A';
     96             if(temp[1] != '-') {
     97                 int fa = temp[1] - 'A';
     98                 father[child] = fa;
     99                 relation[child][fa] = 1;
    100             }
    101             if(temp[2] != '-') {
    102                 int ma = temp[2] - 'A';
    103                 mother[child] = ma;
    104                 relation[child][ma] = 1;
    105             }
    106             
    107         }
    108         for(int i = 0; i < m; i++) {
    109             scanf("%s",temp);
    110             if(temp[0] == '-' || temp[1] == '-') {
    111                 puts("-");
    112                 continue;
    113             }
    114             int p1 = temp[0] - 'A';
    115             int p2 = temp[1] - 'A';
    116             int ans1 = find(p1, p2);
    117             if(ans1 == 0) {
    118                 int ans2 = find(p2,p1);
    119                 prinfop(ans2);
    120             }
    121             else {
    122                 prinfoc(ans1);
    123             }
    124         }
    125         scanf("%d %d",&n, &m);
    126 
    127     }   
    128     return 0;
    129 }

    这道题用了二叉树的遍历和动态规划的思想,最重要的是一遍通过了(忽略第一次忘记注释freopen),开心!

  • 相关阅读:
    TreeSet和TreeMap中“相等”元素可能并不相等
    求众数——摩尔投票
    5802. 统计好数字的数目
    快速幂
    LCP 07.传递消息
    332. 重新安排行程(欧拉回路问题)
    126. 单词接龙 II
    879. 盈利计划
    287. 寻找重复数
    239. 滑动窗口最大值
  • 原文地址:https://www.cnblogs.com/jasonJie/p/5689133.html
Copyright © 2011-2022 走看看