• PTA数据结构与算法题目集(中文) 7-27


    PTA数据结构与算法题目集(中文)  7-27

    7-27 家谱处理 (30 分)
     

    人类学研究对于家族很感兴趣,于是研究人员搜集了一些家族的家谱进行研究。实验中,使用计算机处理家谱。为了实现这个目的,研究人员将家谱转换为文本文件。下面为家谱文本文件的实例:

    John
      Robert
        Frank
        Andrew
      Nancy
        David
    

    家谱文本文件中,每一行包含一个人的名字。第一行中的名字是这个家族最早的祖先。家谱仅包含最早祖先的后代,而他们的丈夫或妻子不出现在家谱中。每个人的子女比父母多缩进2个空格。以上述家谱文本文件为例,John这个家族最早的祖先,他有两个子女RobertNancyRobert有两个子女FrankAndrewNancy只有一个子女David

    在实验中,研究人员还收集了家庭文件,并提取了家谱中有关两个人关系的陈述语句。下面为家谱中关系的陈述语句实例:

    John is the parent of Robert
    Robert is a sibling of Nancy
    David is a descendant of Robert
    

    研究人员需要判断每个陈述语句是真还是假,请编写程序帮助研究人员判断。

    输入格式:

    输入首先给出2个正整数N(2)和M(≤),其中N为家谱中名字的数量,M为家谱中陈述语句的数量,输入的每行不超过70个字符。

    名字的字符串由不超过10个英文字母组成。在家谱中的第一行给出的名字前没有缩进空格。家谱中的其他名字至少缩进2个空格,即他们是家谱中最早祖先(第一行给出的名字)的后代,且如果家谱中一个名字前缩进k个空格,则下一行中名字至多缩进k+2个空格。

    在一个家谱中同样的名字不会出现两次,且家谱中没有出现的名字不会出现在陈述语句中。每句陈述语句格式如下,其中XY为家谱中的不同名字:

    X is a child of Y
    X is the parent of Y
    X is a sibling of Y
    X is a descendant of Y
    X is an ancestor of Y
    

    输出格式:

    对于测试用例中的每句陈述语句,在一行中输出True,如果陈述为真,或False,如果陈述为假。

    输入样例:

    6 5
    John
      Robert
        Frank
        Andrew
      Nancy
        David
    Robert is a child of John
    Robert is an ancestor of Andrew
    Robert is a sibling of Nancy
    Nancy is the parent of Frank
    John is a descendant of Andrew
    

    输出样例:

    True
    True
    True
    False
    False
    题目分析:一道利用树的基础题 主要就是对数据的处理 然后对各种操作的实现 树利用孩子兄弟法存储 对每一个树节点元素 添加一个Level元素 进行对其位置判别 比如空格空2位 其level就是2 注意将其添加到树中时 要去掉空格
    (os:这道题没全队 后两个测试点显示 我段错误 找了一会都没找出来哪里越界了)
      1 #define _CRT_SECURE_NO_WARNINGS
      2 #include<stdio.h>
      3 #include<string.h>
      4 #include<malloc.h>
      5 
      6 typedef struct TreeNode* PtrToTreeNode;
      7 typedef PtrToTreeNode Parent;
      8 typedef PtrToTreeNode NextSibiling;
      9 typedef PtrToTreeNode FirstChild;
     10 struct TreeNode
     11 {
     12     char Data[1000];
     13     NextSibiling sibiling;
     14     FirstChild firstChild;
     15     int Level;
     16 };
     17 void ChangeStr(int i, char name[])
     18 {
     19     int k = strlen(name);
     20     int j;
     21     for (j = 0; j < k - i; j++)
     22         name[j] = name[j + i];
     23     for (; j < k; j++)
     24         name[j] = '';
     25 }
     26 
     27 PtrToTreeNode Insert(char name[],PtrToTreeNode Tree)
     28 {
     29     int i = 0;
     30     for (i = 0; name[i] == ' '; i++)
     31         ;
     32     //i /= 2;
     33     if (!Tree)
     34     {
     35         Tree = (PtrToTreeNode)malloc(sizeof(struct TreeNode));
     36         ChangeStr(i, name);
     37         strcpy(Tree->Data, name);
     38         Tree->firstChild = NULL;
     39         Tree->sibiling = NULL;
     40         Tree->Level = i;
     41     }
     42     else
     43     {
     44         if (i > Tree->Level)
     45         {
     46             if (!Tree->sibiling)
     47                 Tree->firstChild = Insert(name, Tree->firstChild);
     48             else
     49                 Tree->sibiling = Insert(name, Tree->sibiling);
     50         }
     51         else if (i == Tree->Level)
     52             Tree->sibiling = Insert(name, Tree->sibiling);
     53     }
     54     return Tree;
     55 }
     56 
     57 PtrToTreeNode FindElement(PtrToTreeNode Tree, char a[])
     58 {
     59     PtrToTreeNode Tmp;
     60     if (Tree)
     61     {
     62         if (!strcmp(Tree->Data, a))
     63             return Tree;
     64         if (Tmp = FindElement(Tree->firstChild, a))
     65             return Tmp;
     66         if (Tmp = FindElement(Tree->sibiling, a))
     67             return Tmp;
     68     }
     69     return NULL;
     70 }
     71 
     72 int FindChild(PtrToTreeNode Tree,char a[], char c[])
     73 {
     74     PtrToTreeNode Parent = FindElement(Tree, c);
     75     PtrToTreeNode Position = Parent->firstChild;
     76     if (!strcmp(Position->Data, a))
     77         return 1;
     78     while (Position->sibiling)
     79     {
     80         Position = Position->sibiling;
     81         if (!strcmp(Position->Data, a))
     82             return 1;
     83     }
     84     return 0;
     85 }
     86 
     87 int FindAncestor(PtrToTreeNode Ancestor,char c[])
     88 {
     89     if(Ancestor)
     90     {
     91         if (!strcmp(Ancestor->Data, c))
     92             return 1;
     93         if (FindAncestor(Ancestor->firstChild, c) || FindAncestor(Ancestor->sibiling, c))
     94             return 1;
     95         else
     96             return 0;
     97     }
     98     return 0;
     99 }
    100 
    101 int FindBrother(PtrToTreeNode T, PtrToTreeNode Bro)
    102 {
    103     while (T)
    104     {
    105         if (T == Bro)
    106             return 1;
    107         else
    108             T = T->sibiling;
    109     }
    110     return 0;
    111 }
    112 
    113 int FindSibiling(PtrToTreeNode Tree, char a[], char b[])
    114 {
    115     PtrToTreeNode Brother1 = FindElement(Tree, a);
    116     PtrToTreeNode Brother2 = FindElement(Tree, b);
    117     if (FindBrother(Brother1, Brother2) || FindBrother(Brother2, Brother1))
    118         return 1;
    119     else
    120         return 0;
    121 }
    122 
    123 int Judget(PtrToTreeNode Tree,char a[], char b[], char c[])
    124 {
    125     PtrToTreeNode Ancestor = NULL;
    126     switch (b[0])
    127     {
    128     case'c':return FindChild(Tree, a, c); break;
    129     case'a':Ancestor = FindElement(Tree,a); return FindAncestor(Ancestor, c); break;
    130     case's':return FindSibiling(Tree, a, c); break;
    131     case'p':return FindChild(Tree, c, a); break;
    132     case'd':Ancestor = FindElement(Tree, c); return FindAncestor(Ancestor,a); break;
    133     }
    134 }
    135 
    136 int main()
    137 {
    138     int N, M;
    139     scanf("%d%d
    ", &N, &M);
    140     PtrToTreeNode Tree=NULL;
    141     while (N--)
    142     {
    143         char name[1000];
    144         gets(name);
    145         Tree=Insert(name, Tree);
    146     }
    147     while (M--)
    148     {
    149         char a[1000], b[1000], c[1000], d[1000], e[1000],f[1000];
    150         scanf("%s%s%s%s%s%s",a, b, c, d, e,f);
    151         if (Judget(Tree,a, d, f))
    152             printf("True
    ");
    153         else
    154             printf("False
    ");
    155     }
    156     return 0;
    157 }
    View Code
  • 相关阅读:
    淡入淡出js
    Comparable和Comparator的区别
    mybatis的动态sql详解
    mybatis动态sql之foreach
    mybatis的动态sql中collection与assoction
    Mybatis中#与$区别
    转JSONObject put,accumulate,element的区别
    Spring配置,JDBC数据源及事务
    销毁session
    IIS express 7.5 设置默认文档
  • 原文地址:https://www.cnblogs.com/57one/p/11638362.html
Copyright © 2020-2023  润新知