Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- In this program you are given a series of births, indicating the name and gender of the child. You are then given
- a name of someone who wants to become the new monarch of England. You are to output the number of people who
- have to die for that person to inherit the throne of england.
- Input
- The first line of the input will contain a positive integer b representing the number of births that the program will
- contain. The next b lines will each document a single birth chronologically. Each birth will of the form parent's
- name, child's name, and sex (where sex is a single letter either f or m), each separated by a single space. The names
- will contain no spaces. The first parent mentioned will be considered the reigning monarch.
- This will be followed by a positive integer q representing the number of queries to be made. Each of the next q
- lines will contain the name of a single royal.
- Output
- For each query name q, print on 1 line the number of people who need to die for the qth name to become the ruler.
- Sample Input
- 16
- elizabethII charles m
- elizabethII anne f
- elizabethII andrew m
- elizabethII edward m
- charles william m
- anne peter m
- anne zara f
- charles harry m
- andrew beatrice f
- andrew eugenie f
- edward louise f
- edward severn m
- william george m
- peter savannah f
- peter isla f
- zara mia f
- 2
- george
- peter
- Corresponding Sample Output
- 3
- 12
- struct Person {
- string parent;
- string name;
- char sex;
- int numDescendants;
- Person (string nam ="", char sx = "m", string par ="");// sets the name,sex, and the parent's name
- };
- class Royals {
- Person family[1000];
- int numMembers; // holds the current number of people in the royal family
- void upDateDecents(string name);// updates the number of decedents of a person just added, called from add
- public :
- Royals(); // constructor
- add(Person person); // adds a person to the array, updates the number of Members and the ancestors list,
- int howMany2Die(string mName);// returns the number of people who must die, so mName can become the monarch
- };
- Algorithm
- The number of people that have to die (if you are the current monarch) is 0 The number of people that have to die if
- you are a male is The number of number of people your parent needs to kill to inherit + the number of male siblings
- and their descendants born before you The number of people that have to die if you are a female is The number of
- number of people your parent needs to kill to inherit + the number of male siblings + the number of female siblings
- who were born before you + all of those siblings descendants
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement