Shrew-ology

Interview

Time limit1sMemory limit128 MB

Summary
Given per-trait dominance rules and adult shrews with sex and trait bits, list every mother-father pair that could produce each juvenile.
Level

Medium4 of 10

Topics
Brute force, Implementation, String, Sorting
Solved
No attempts yet

Problem

Dr. Montgomery Moreau has spent years observing a wild population of Northern Madagascar pie-bald shrews. He has carefully catalogued every shrew in the area, recorded its distinctive physical characteristics, and given each animal a name.

For every characteristic (for example: brown fur, red eyes, white feet, or prominent incisor teeth) he noted whether it is dominant or recessive:

  • Dominant: if either parent has the characteristic, the child has it too. In other words, the child has a dominant characteristic exactly when at least one parent has it.
  • Recessive: the child has the characteristic only if both parents have it. In other words, the child has a recessive characteristic exactly when both parents have it.

His funding lapsed and he was away for several months, during which a new generation was born. Now that he has returned, he wants to determine, for each juvenile, every pair of adults that could be its parents.

A juvenile's parents are exactly one adult female (the mother) and one adult male (the father). A (mother, father) pair could be the parents of a juvenile if, for every characteristic, the rule above applied to the two adults yields exactly the juvenile's value for that characteristic.

Input

The first line contains a string of 1 to 80 consecutive D and R characters. The i-th character states whether the i-th characteristic is dominant (D) or recessive (R).

Each of the following lines describes one adult shrew: a name of 1 to 32 non-blank characters, a single space, a single character M or F giving the animal's sex, another single space, and finally a string of 0/1 characters (one per characteristic, in order) in which 1 means the animal has that characteristic and 0 means it does not. The list of adults ends with a line containing only ***.

After that, one or more lines describe the juvenile shrews. Each juvenile line contains a name and a 0/1 characteristic string in the same format, separated by a single space, but without a sex field (a juvenile's sex is irrelevant). The list of juveniles ends with a line containing only ***.

Output

For each juvenile, in the order given, print one line: the juvenile's name, the text by, and then the list of all pairs of adults that could be its parents. Write each pair as the mother's name, a hyphen, and then the father's name.

If more than one pair is possible, sort the pairs in lexicographic order first by the mother's name and then, among pairs with the same mother, by the father's name; separate consecutive pairs with the text or. If no pair of adults could be the parents, print the juvenile's name followed by by with nothing after it.

Examples5

  1. Example 1

    Input
    RDDR
    Speedy M 0101
    Jumper F 0101
    Slowpoke M 1101
    Terror F 1100
    Shadow F 1001
    ***
    Frisky 0101
    Sleepy 1101
    ***
    
    Expected output
    Frisky by Jumper-Slowpoke or Jumper-Speedy or Shadow-Speedy
    Sleepy by Shadow-Slowpoke
    
  2. Example 2

    Input
    D
    A M 1
    B M 0
    C F 1
    D F 0
    ***
    J1 1
    J2 0
    ***
    
    Expected output
    J1 by C-A or C-B or D-A
    J2 by D-B
    
  3. Example 3

    Input
    R
    A M 1
    B M 0
    C F 1
    D F 0
    ***
    J1 1
    J2 0
    ***
    
    Expected output
    J1 by C-A
    J2 by C-B or D-A or D-B
    
  4. Example 4

    Input
    DDD
    Rex M 111
    Max M 100
    Luna F 010
    Nova F 001
    ***
    Pup 111
    Cub 010
    ***
    
    Expected output
    Pup by Luna-Rex or Nova-Rex
    Cub by 
    
  5. Example 5

    Input
    DR
    Al M 11
    Alex M 10
    Albert M 01
    Bella F 11
    Bianca F 10
    ***
    Kid 11
    Tot 10
    Wee 00
    ***
    
    Expected output
    Kid by Bella-Al or Bella-Albert
    Tot by Bella-Alex or Bianca-Al or Bianca-Albert or Bianca-Alex
    Wee by