Given two positions in a binary family tree and the second person's gender, print the English kinship term of the second to the first.
Medium5TreeMathImplementationNo attempts yetTime limit1sMemory limit256 MBEnglish has a large vocabulary for family relationships. The base relationships are:
Later generations use these extensions:
Write a program that reports the relationship of one person to another.
The input holds one or more datasets. Each dataset is one line with two integers A and B (0≤A,B≤32767) and one character. A line whose first integer is negative marks the end of the input. That line is not a dataset and produces no output.
The two integers identify person A and person B. The character is M or F and gives the gender of person B, male or female.
The integers are positions in a family tree drawn as a full binary tree. The root is numbered 0, its children are numbered 1 and 2, the children of 1 are 3 and 4, the children of 2 are 5 and 6, and the numbering continues level by level, left to right. The diagram below shows the numbering. A parent-child link in this tree is a parent-child link in the family.

For each dataset print one line with the relationship of B to A. Build the answer only from these phrases: "self", "sister", "brother", "daughter", "son", "mother", "father", "niece", "nephew", "aunt", "uncle", "cousin", "grand", "great-", "1st", "2nd", "3rd", "once removed", "twice removed", "thrice removed". At most two "great-" prefixes may be applied. If "1st", "2nd" or "3rd" is used, a single blank separates it from the rest of the line. If "once removed", "twice removed" or "thrice removed" is used, a single blank separates it from the part before it. If the relationship of B to A cannot be written under these limits, print "kin".