Generic Units Conversion

Time limit1sMemory limit128 MB

Summary
Parse two measurement systems with internal conversion rules, then convert each quantity so every unit of the second system appears with a greedy integer count, rounding the smallest unit.
Level

Medium6 of 10

Topics
Implementation, Simulation, Math, String
Solved
No attempts yet

Problem

Design a program that reads the description of two systems of measurement for a common physical quantity (length, weight, area, time, and so on), a rule that converts between the two systems, and a quantity expressed in the first system, and then expresses that same quantity in the second system.

Input

The input contains one or more problem sets. Each problem set describes two systems of measurement, one conversion rule between them, and a list of quantities to convert.

Each problem set has the following structure.

  1. A line naming the units of the first system, from the largest unit to the smallest, separated by single spaces. This line is at most 80 characters long. Each unit name consists only of alphabetic characters, and no name is repeated on the line.
  2. If the first system has NN units, the next N−1N-1 lines give internal conversion rules in the form a unit1 = b unit2, where aa and bb are positive numbers (integer or decimal) and unit1, unit2 are units of the first system. These N−1N-1 rules always provide enough information to convert between any two units of the system.
  3. The second system is described immediately afterward in the same format (its unit line followed by its N−1N-1 internal rules).
  4. One conversion rule in the same a unit1 = b unit2 form, where unit1 belongs to the first system and unit2 belongs to the second system.
  5. One or more quantity lines follow. A quantity is written as one or more (number, unit) pairs; within a quantity the units appear in decreasing order of size, though not every unit of the system need appear. Every number is non-negative.

A completely empty line marks the end of the list of quantities and of that problem set. If the line after that empty line is non-empty, another problem set begins; if it is also empty, the input ends.

All values stay within ranges for which every output number fits in a normal (32-bit) integer.

Output

For each quantity, print one line giving the equivalent quantity in the second system. List every unit of the second system, from largest to smallest, including any unit whose count is zero. Choose the counts greedily so that the larger units absorb as much of the value as possible. Every count is an integer, and the count of the smallest unit is rounded to the nearest integer (exact halves round up). Separate every number and unit name by a single space.

Examples2

  1. Example 1

    Input
    miles yards feet
    5280 feet = 1 miles
    3 feet = 1 yards
    km m cm
    1000 m = 1 km
    0.01 m = 1 cm
    1 feet = 30.48 cm
    2 miles 1 feet
    0.0833 feet
    
    furlongs fathoms
    1 furlongs = 110 fathoms
    feet inches
    12 inches = 1 feet
    1 fathoms = 6 feet
    1 furlongs
    0.5 furlongs 0.25 fathoms
    
    
    
    Expected output
    3 km 218 m 99 cm
    0 km 0 m 3 cm
    660 feet 0 inches
    331 feet 6 inches
    
  2. Example 2

    Input
    kg
    lb
    1 kg = 2 lb
    5 kg
    3 kg
    
    
    Expected output
    10 lb
    6 lb