java数据结构41:家谱处理

41:家谱处理

总时间限制: 

1000ms

内存限制: 

65535kB

描述

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

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

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

John is the parent of Robert

David is a descendant of Robert

Robert is a sibling of Nancy

研究人员需要判断每个陈述语句是真还是假,请编写程序帮助研究人员判断。为简化程序,我们约定测试数据中出现的每个人,其子女不超过2个。

输入

输入首先给出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

提示

为简化程序,我们约定测试数据中出现的每个人,其子女不超过2个。

实现代码如下:


import java.util.ArrayList;
import java.util.Scanner;

/**
 * @author baikunlong
 * @date 2020/6/22 23:45
 */
public class Main {

    private static ArrayList<Person> list;

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int m = scanner.nextInt();
        scanner.nextLine();
        list = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            String name = scanner.nextLine();
            int kongGe = getKongGe(name);
            list.add(new Person(kongGe, name.trim(), i));
        }

        ArrayList<String> qList = new ArrayList<>();
        for (int i = 0; i < m; i++) {
            qList.add(scanner.nextLine());
        }

        for (int i = 0; i < qList.size() - 1; i++) {
            show(qList, i);
        }
        show(qList, qList.size() - 1);
    }

    private static void show(ArrayList<String> qList, int i) {
        String s = qList.get(i);
        String[] strings = s.split(" ");
        Person person1 = getPersonKongGeByName(strings[0].trim());
        Person person2 = getPersonKongGeByName(strings[strings.length - 1].trim());
        if (i != qList.size() - 1) {
            if (s.contains("is a child of")) {
                if (person1.kongGe - person2.kongGe == 2) {
                    if (person1.index - person2.index <= 2 && person1.index - person2.index > 0) {
                        System.out.println("True");
                    } else {
                        System.out.println("False");
                    }
                } else {
                    System.out.println("False");
                }
            } else if (s.contains("is the parent of")) {
                if (person2.kongGe - person1.kongGe == 2) {
                    if (person2.index - person1.index <= 2 && person2.index - person1.index > 0) {
                        System.out.println("True");
                    } else {
                        System.out.println("False");
                    }
                } else {
                    System.out.println("False");
                }
            } else if (s.contains("is a sibling of")) {
                if (person1.kongGe == person2.kongGe) {
                    int p1Father = 0, p2Father = 0;
                    for (int j = 0; j < person1.index; j++) {
                        if (person1.kongGe - list.get(j).kongGe == 2) {
                            p1Father = j;
                        }
                    }
                    for (int k = 0; k < person2.index; k++) {
                        if (person2.kongGe - list.get(k).kongGe == 2) {
                            p2Father = k;
                        }
                    }
                    if (p1Father == p2Father) {
                        System.out.println("True");
                    } else {
                        System.out.println("False");
                    }
                } else {
                    System.out.println("False");
                }
            } else if (s.contains("is a descendant of")) {
                if (person1.kongGe > person2.kongGe) {
                    System.out.println("True");
                } else {
                    System.out.println("False");
                }
            } else if (s.contains("is an ancestor of ")) {
                if (person1.kongGe < person2.kongGe) {
                    System.out.println("True");
                } else {
                    System.out.println("False");
                }
            }
        }else {
            if (s.contains("is a child of")) {
                if (person1.kongGe - person2.kongGe == 2) {
                    if (person1.index - person2.index <= 2 && person1.index - person2.index > 0) {
                        System.out.print("True");
                    } else {
                        System.out.print("False");
                    }
                } else {
                    System.out.print("False");
                }
            } else if (s.contains("is the parent of")) {
                if (person2.kongGe - person1.kongGe == 2) {
                    if (person2.index - person1.index <= 2 && person2.index - person1.index > 0) {
                        System.out.print("True");
                    } else {
                        System.out.print("False");
                    }
                } else {
                    System.out.print("False");
                }
            } else if (s.contains("is a sibling of")) {
                if (person1.kongGe == person2.kongGe) {
                    int p1Father = 0, p2Father = 0;
                    for (int j = 0; j < person1.index; j++) {
                        if (person1.kongGe - list.get(j).kongGe == 2) {
                            p1Father = j;
                        }
                    }
                    for (int k = 0; k < person2.index; k++) {
                        if (person2.kongGe - list.get(k).kongGe == 2) {
                            p2Father = k;
                        }
                    }
                    if (p1Father == p2Father) {
                        System.out.print("True");
                    } else {
                        System.out.print("False");
                    }
                } else {
                    System.out.print("False");
                }
            } else if (s.contains("is a descendant of")) {
                if (person1.kongGe > person2.kongGe) {
                    System.out.print("True");
                } else {
                    System.out.print("False");
                }
            } else if (s.contains("is an ancestor of ")) {
                if (person1.kongGe < person2.kongGe) {
                    System.out.print("True");
                } else {
                    System.out.print("False");
                }
            }
        }
    }

    private static Person getPersonKongGeByName(String name) {
        for (Person person : list) {
            if (person.name.equals(name)) {
                return person;
            }
        }
        return null;
    }

    private static int getKongGe(String name) {
        int n = 0;
        char[] chars = name.toCharArray();
        for (char aChar : chars) {
            if (aChar == ' ') {
                n++;
            }
        }
        return n;
    }

    static class Person {
        int kongGe;
        String name;
        int index;

        public Person(int kongGe, String name, int index) {
            this.kongGe = kongGe;
            this.name = name;
            this.index = index;
        }

        @Override
        public String toString() {
            return "Person{" +
                    "kongGe=" + kongGe +
                    ", name='" + name + '\'' +
                    ", index=" + index +
                    '}';
        }
    }
}

猜你喜欢

转载自blog.csdn.net/baikunlong/article/details/106931048