7-5 家谱处理(30 分) 【数据结构】

7-5 家谱处理(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

思路
找父母的方法 有两个
0.可以往前遍历 遇到第一个空格数小于他的 就是 他的 父母
1.可以用一个数组来维护 当前空格数/2 的那个人 那么 当前空格数/2 - 1 的人就是他父母

然后要注意
0.sibling 是 父母相同的人 而不是 空格数相同的人
1.ancestor 是 父母或 父母的父母的父母。。。 而不是单单的 空格数 小于 就可以了

AC代码

#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits>

#define CLR(a) memset(a, 0, sizeof(a))
#define pb push_back

using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair<string, int> psi;
typedef pair<string, string> pss;

const double PI = 3.14159265358979323846264338327;
const double E = exp(1);
const double eps = 1e-30;

const int INF = 0x3f3f3f3f;
const int maxn = 1e2 + 5;
const int MOD = 1e9 + 7;

int pre[maxn];

struct Node
{
    string s;
    string p;
    int k;
}q[maxn];

map <string, int> vis; 

string w[maxn];

void init()
{
    for (int i = 0; i < maxn; i++)
        pre[i] = i;
}


int main()
{
    init();
    int n, m, k = 0;
    scanf("%d%d ", &n, &m);
    string s, temp;
    getline(cin, temp);
    q[1].s = temp;
    q[1].k = k;
    w[0] = temp;
    vis[temp] = 1;
    pre[1] = -1; 
    int len;
    for (int i = 2; i <= n; i++)
    {
        getline(cin, s);
        k = 0;
        len = s.size();
        temp.clear();
        for (int j = 0; j < len; j++)
        {
            if (s[j] == ' ')
                k++;
            else
                temp += s[j];
        }
        q[i].s = temp;
        vis[temp] = i;
        q[i].k = k;
        pre[i] = vis[w[k / 2 - 1]];
        w[k / 2] = temp;
    }
    string a, b;
    for (int i = 0; i < m; i++)
    {
        cin >> a;
        cin >> s;
        cin >> s;
        if (s == "a")  // 1 3 5
        {
            cin >> s;
            if (s == "child") // 1
            {
                cin >> s;
                cin >> b;
                int id_a = vis[a], id_b = vis[b];
                if (pre[id_a] == id_b)
                    printf("True
");
                else
                    printf("False
");
            }
            else if (s == "sibling") // 3
            {
                cin >> s;
                cin >> b;
                int id_a = vis[a], id_b = vis[b];
                if (pre[id_a] == pre[id_b] && id_a != id_b)
                    printf("True
");
                else
                    printf("False
");
            }
            else if (s == "descendant") // 5
            {
                cin >> s;
                cin >> b;
                int id_a = vis[a], id_b = vis[b];
                while (pre[id_a] != id_b && id_a != 1) 
                    id_a = pre[id_a];
                if (id_a != 1)
                    printf("True
");
                else
                    printf("False
");
            }
        }
        else if (s == "an") // 2
        {
            cin >> s;
            cin >> s;
            cin >> b;
            int id_a = vis[a], id_b = vis[b];
            while (pre[id_b] != id_a && id_b != 1)
                id_b = pre[id_b];
            if (id_b != 1)
                printf("True
");
            else
                printf("False
");
        }
        else if (s == "the") // 4
        {
            cin >> s;
            cin >> s;
            cin >> b;
            int id_a = vis[a], id_b = vis[b];
            if (pre[id_b] == id_a)
                printf("True
");
            else
                printf("False
");
        }
    }
}






原文地址:https://www.cnblogs.com/Dup4/p/9433184.html