数据结构PTA习题:进阶实验4-3.1 家谱处理 (30分)

进阶实验4-3.1 家谱处理 (30分)

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

John
Robert
Frank
Andrew
Nancy
David

家谱文本文件中,每一行包含一个人的名字。第一行中的名字是这个家族最早的祖先。家谱仅包含最早祖先的后代,而他们的丈夫或妻子不出现在家谱中。每个人的子女比父母多缩进2个空格。以上述家谱文本文件为例,John这个家族最早的祖先,他有两个子女Robert和Nancy,Robert有两个子女Frank和Andrew,Nancy只有一个子女David。
在实验中,研究人员还收集了家庭文件,并提取了家谱中有关两个人关系的陈述语句。下面为家谱中关系的陈述语句实例:

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

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

输入格式:
输入首先给出2个正整数N(2≤N≤100)和M(≤100),其中N为家谱中名字的数量,M为家谱中陈述语句的数量,输入的每行不超过70个字符。
名字的字符串由不超过10个英文字母组成。在家谱中的第一行给出的名字前没有缩进空格。家谱中的其他名字至少缩进2个空格,即他们是家谱中最早祖先(第一行给出的名字)的后代,且如果家谱中一个名字前缩进k个空格,则下一行中名字至多缩进k+2个空格。
在一个家谱中同样的名字不会出现两次,且家谱中没有出现的名字不会出现在陈述语句中。每句陈述语句格式如下,其中X和Y为家谱中的不同名字:

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

起初我使用构建一个用数组存储的完全二叉树,最后一个测试点一直无法通过,可能是因为结构变成了斜二叉树时,即使只有100个元素,数组存储下标也会变成2^99,很大很大…
最后的编程想法来源于跃鱼的一篇博客(原文链接:https://www.cnblogs.com/snzhong/p/12589483.html),主要思想如下:
用结构体存储每个结点的数据,其中包含一个parent记录父结点的下标,space记录该数据有多少个空格。
创建一个结构体数组,依次存入所有的结点数据。
遍历数组内的每个结构体,从当前结构体往前寻找,找到第一个空格数比它少2个的最近的结构体,该结构体就是当前结构体的父结点,将其下标记录给当前结构体的parent。
判断字符串c1(结构体1)是否为字符串c2(结构体2)的孩子:判断结构体1的parent是否等于结构体2在数组内的下标;
判断字符串c1(结构体1)是否为字符串c2(结构体2)的祖先:循环判断结构体2的parent是否等于结构体1在数组内的下标,直至结构体2在数组内的下标变为0;
判断字符串c1(结构体1)是否为字符串2(结构体2)的兄弟:判断结构体1的parent是否等于结构体2的parent;
判断字符串c1(结构体1)是否为字符串2(结构体2)的父亲:判断结构体2的parent是否等于结构体1在数组内的下标;
判断字符串c1(结构体1)是否为字符串2(结构体2)的后代:循环判断结构体1的parent是否等于结构体2在数组内的下标,直至结构体1在数组内的下标变为0;

C语言实现:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct people
{
 char *name;
 int parent;//记录父结点下标
 int space;//记录空格数
};
typedef struct people People;
People * s;//结构体数组
int N;
int Find(char * c1);//找到字符串c1在结构体数组内对应的下标
int child(char * c1, char * c2);//判断字符串c1是否为字符串c2的孩子
int ancestor(char * c1, char * c2);//判断字符串c1是否为字符串c2的祖先
int sibling(char * c1, char * c2);//判断字符串c1是否为字符串c2的兄弟
int parent(char * c1, char * c2);//判断字符串c1是否为字符串c2的父亲
int descendant(char * c1, char * c2);//判断字符串c1是否为字符串c2的后代
int main()
{
 int M;
 scanf("%d %d\n", &N, &M);
 int i, j;
 s = (People *)malloc(N * sizeof(struct people));
 for (i = 0; i < N; i++)
 {
  s[i].name = (char *)malloc(11 * sizeof(char));
 }
 char * ch;
 ch = (char *)malloc(71 * sizeof(char));
 //初始化根结点
 scanf("%s", ch);
 strcpy(s[0].name, ch);
 s[0].parent = -1;
 s[0].space = 0;
 char c;
 int sp;
 scanf("%c", &c);//读入换行符
 for (i = 1; i < N; i++)
 {
  scanf("%c", &c);
  j = 0; sp = 0;//sp记录空格数
  while (c != '\n')
  {
   if (c == ' ') { sp++; }
   else { ch[j] = c; j++; }
   scanf("%c", &c);
  }
  ch[j] = '\0';
  strcpy(s[i].name, ch);
  s[i].space = sp;
 }
 //遍历数组内的每个结构体
 //从当前结构体往前寻找
 //找到第一个空格数比它少2个的最近的结构体
 //该结构体就是当前结构体的父结点
 for (i = 1; i < N; i++)
 {
  for (j = i - 1; j >= 0; j--)
  {
   if (s[i].space == s[j].space + 2) { break; }
  }
  s[i].parent = j;
 }
 char * c1; char * c2; int ret;
 c1 = (char *)malloc(11 * sizeof(char));
 c2 = (char *)malloc(11 * sizeof(char));
 for (i = 0; i < M; i++)
 {
  scanf("%s", c1);//读入字符串c1
  scanf("%c", &c);//读入空格
  scanf("%s", ch);
  scanf("%c", &c);//读入空格
  scanf("%s", ch);
  if (strcmp(ch, "an") == 0)
  {
   scanf("%c", &c);//读入空格
   scanf("%s", ch);
   scanf("%c", &c);//读入空格
   scanf("%s", ch);
   scanf("%c", &c);//读入空格
   scanf("%s", c2);//读入字符串c2
   ret = ancestor(c1, c2);
   if (ret == 1) { printf("True\n"); }
   else { printf("False\n"); }
  }
  else if (strcmp(ch, "the") == 0)
  {
   scanf("%c", &c);//读入空格
   scanf("%s", ch);
   scanf("%c", &c);//读入空格
   scanf("%s", ch);
   scanf("%c", &c);//读入空格
   scanf("%s", c2);//读入字符串c2
   ret = parent(c1, c2);
   if (ret == 1) { printf("True\n"); }
   else { printf("False\n"); }
  }
  else if (strcmp(ch, "a") == 0)
  {
   scanf("%c", &c);//读入空格
   scanf("%s", ch);
   if (strcmp(ch, "child") == 0)
   {
    scanf("%c", &c);//读入空格
    scanf("%s", ch);
    scanf("%c", &c);//读入空格
    scanf("%s", c2);//读入字符串c2
    ret = child(c1, c2);
    if (ret == 1) { printf("True\n"); }
    else { printf("False\n"); }
   }
   else if (strcmp(ch, "sibling") == 0)
   {
    scanf("%c", &c);//读入空格
    scanf("%s", ch);
    scanf("%c", &c);//读入空格
    scanf("%s", c2);//读入字符串c2
    ret = sibling(c1, c2);
    if (ret == 1) { printf("True\n"); }
    else { printf("False\n"); }
   }
   else if (strcmp(ch, "descendant") == 0)
   {
    scanf("%c", &c);//读入空格
    scanf("%s", ch);
    scanf("%c", &c);//读入空格
    scanf("%s", c2);//读入字符串c2
    ret = descendant(c1, c2);
    if (ret == 1) { printf("True\n"); }
    else { printf("False\n"); }
   }
  }
 }
 return 0;
}
//找到字符串c1在结构体数组内对应的下标
int Find(char * c1)
{
 int i = 0;
 for (; i < N; i++)
 {
  if (strcmp(s[i].name, c1) == 0) { return i; }
 }
}
//判断字符串c1是否为字符串c2的孩子
int child(char * c1, char * c2)
{
 int index1, index2;
 index1 = Find(c1);
 index2 = Find(c2);
 if (s[index1].parent == index2) { return 1; }
 else { return 0; }
}
//判断字符串c1是否为字符串c2的祖先
int ancestor(char * c1, char * c2)
{
 int index1, index2;
 index1 = Find(c1);
 index2 = Find(c2);
 while (index2 > 0)
 {
  if (s[index2].parent == index1) { return 1; }
  index2 = s[index2].parent;
 }
 return 0;
}
//判断字符串c1是否为字符串c2的兄弟
int sibling(char * c1, char * c2)
{
 int index1, index2;
 index1 = Find(c1);
 index2 = Find(c2);
 if (s[index1].parent == s[index2].parent)
 {
  return 1;
 }
 else { return 0; }
}
//判断字符串c1是否为字符串c2的父亲
int parent(char * c1, char * c2)
{
 int index1, index2;
 index1 = Find(c1);
 index2 = Find(c2);
 if (s[index2].parent == index1) { return 1; }
 else { return 0; }
}
//判断字符串c1是否为字符串c2的后代
int descendant(char * c1, char * c2)
{
 int index1, index2;
 index1 = Find(c1);
 index2 = Find(c2);
 while (index1 > 0)
 {
  if (s[index1].parent == index2) { return 1; }
  index1 = s[index1].parent;
 }
 return 0;
}
  • 5
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值