We are keeping a family tree. We update the data by adding information as it
becomes available. When information arrives that is not
consistent with the previous data, we need to recognize that fact immediately.
We will enforce only the most obvious rules:-
A person is either male or female.
-
A child's parent cannot be the child itself or a descendant of the child
-
A child has two parents, one male and the other female.
(A person is a descendant only of his parents, grandparents, greatgrandparents,
etc.)
Each piece of data gives either the names of a child and parent or the name of
a person and that person's gender. All occurrences of a name represent the same person.
Create a class FamilyTree that contains a method firstBad that is given a
String[] data. The method returns the (0-based) index of the first element of data that is inconsistent
with the previous elements of data, or returns -1 if all the data is consistent.
Each element of data will be formatted in one of these two forms:
"childname parentname"
"name gender"
where the two parts are separated by a single space character, each name is all
uppercase letters 'A'-'Z' and gender is a single lowercase letter, either 'm' or 'f'.
|