数据结构—— 家谱处理

版权声明:转载请注明 https://blog.csdn.net/NCC__dapeng/article/details/83866690

这是一道PTA中的题目,在这里先给出题干。

7-2 家谱处理 (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≤N≤100)和M(≤100),其中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

 一道模拟题,讲思路之前,先带着我自己捋一下啥叫descendant(后代)、ancestor(祖先)。

ancestor(祖先):简单的来说就是你爸爸、你爷爷。。。那些你叔叔啥的都不算。

descendant(后代):和上面差不多,就是说你孙子,你儿子,那些叫你叔叔舅舅啥的都不算。

这两个概念,就懵逼了一会,语文是硬伤,下面开始说这道题的思路。

一道模拟题,用数据结构储存数据进行查找,利用string的各种函数对字符串进行操作(注意:PTA为了安全起见是禁止使用gets函数的,所以才有了下面代码对输入处理的那一段)

推荐自己写一下,思路不难,具体的实现细节需要好好去卡一下。(还有备注一定要及时的注释掉,否则呜呜呜)

下面给出AC代码:

​
#include <bits/stdc++.h>

using namespace std;

struct node
{
    char name[100];
    char father[100];
    int num;
} ple[1000];


int main()
{
    int m,n;
    scanf("%d %d",&m,&n);
    getchar();

    for(int i=0; i<m; i++)
    {
        char t[100];
        string s; getline(cin,s);
        int len=s.size();
        for(int j=0;j<len;j++)
        {
            t[j]=s[j];
        }
        t[len]=0;
        int len1=0;

        for(int j=0; j<len; j++)
        {
            if(t[j]!=' ')
            {
                len1=j;
                ple[i].num=j;
                strcpy(ple[i].name,t+j);
                break;
            }
        }

        if(len1==0) strcpy(ple[i].father,"root");
        else
        {
            for(int j=i-1; j>=0; j--)
            {
                if(ple[i].num>ple[j].num)
                {
                    strcpy(ple[i].father,ple[j].name);
                    break;
                }
            }
        }
    }

    char s6[100],s1[100],s2[100],s3[100],s4[100],s5[100];
    for(int i=0; i<n; i++)
    {
        scanf("%s %s %s %s %s %s",s1,s2,s3,s4,s5,s6);
        node p1,p2;

        for(int j=0; j<m; j++)
        {
            if(strcmp(s1,ple[j].name)==0)
            {
                p1.num=ple[j].num;
                strcpy(p1.name,ple[j].name);
                strcpy(p1.father,ple[j].father);
                break;
            }
        }
        for(int j=0; j<m; j++)
        {
            if(strcmp(s6,ple[j].name)==0)
            {
                p2.num=ple[j].num;
                strcpy(p2.name,ple[j].name);
                strcpy(p2.father,ple[j].father);
                break;
            }

        }

        if(strcmp(p1.name,p2.name)==0)
        {
            printf("False\n");
            continue;
        }

        if(s4[0]=='c')
        {
            //cout<<"gg1      "<<p1.num<<"   "<<p2.num<<"   "<<p1.father<<"    "<<p2.name<<endl;
            if(strcmp(p2.name,p1.father)==0) printf("True\n");
            else printf("False\n");
        }
        else if(s4[0]=='a')
        {
            if(strcmp(p1.father,"root")==0)
            {
                printf("True\n");
                continue;
            }

            while(strcmp(p1.name,p2.father)!=0&&strcmp(p2.father,"root")!=0)
            {
                for(int k=0;k<m; k++)
                {
                    if(strcmp(p2.father,ple[k].name)==0)
                    {
                        strcpy(p2.father,ple[k].father);
                    }
                }
            }

            if(strcmp(p2.father,"root")==0)  printf("False\n");
            else printf("True\n");
        }
        else if(s4[0]=='s')
        {

            if(strcmp(p1.father,p2.father)==0) printf("True\n");
            else printf("False\n");
        }
        else if(s4[0]=='p')
        {
            if(strcmp(p1.name,p2.father)==0)   printf("True\n");
            else printf("False\n");
        }
        else if(s4[0]=='d')
        {
            //cout<<"gg    "<<p1.father<<endl;
            if(strcmp(p2.father,"root")==0)
            {
                printf("True\n");
                continue;
            }

            while(strcmp(p2.name,p1.father)!=0&&strcmp(p1.father,"root")!=0)
            {
                //cout<<"gg     "<<p1.father<<endl;
                for(int k=0;k<m; k++)
                {
                    if(strcmp(p1.father,ple[k].name)==0)
                    {
                        //cout<<"gg11111     "<<p1.father<<"       "<<ple[i].name<<endl;
                        strcpy(p1.father,ple[k].father);
                    }
                }
            }

            if(strcmp(p1.father,"root")==0)  printf("False\n");
            else printf("True\n");

        }
    }

    return 0;
}

​

猜你喜欢

转载自blog.csdn.net/NCC__dapeng/article/details/83866690
今日推荐