Dimensions
Time limit1sMemory limit256 MB
Convert defined units and evaluate each expression, printing results in SI base units or Incompatible for mismatched dimensions.
- Level
Medium5 of 10
- Topics
- Implementation, Hash map, Math
- Solved
- No attempts yet
Problem
You and your friend Christian are taking a year off to travel around the world, see magnificent places, meet wonderful people and learn new cultures. Different cultures bring different units, and units are the differences that hurt an engineer the most. People measure in miles, firkins, microfortnights, candlepowers, pints and kilowatt hours when the SI base units in the table below would do the job.
Table 1: the SI base units
You like the SI units so much that you refuse to use any other unit. A derived unit such as the joule (J), the newton (N) or the ohm is expressible in base units as kg m^2 / s^2, kg m / s^2 and kg m^2 / s^3 A^2. So during the trip you write down every unit you come across together with its definition. Some definitions depend on earlier ones, like Pa = N / m^2.
With the definitions at hand you no longer put up with 60 firkins / microfortnights or 63 km / h, because you can always convert them to SI units. Calculations like 100 m + 1.3 km and 7 N * 8 ohm become easy too.
You are given the unit definitions and a list of computations. Convert the result of every computation to SI base units.
Input
The input follows this syntax, where ? denotes zero or one, + denotes one or more, and * denotes zero or more.
power ::= { integer from 2 to 4 }
unit ::= { upper or lower case English letter }+
dimension ::= unit ['^' power]?
size ::= { floating-point number } [' ' dimension]* [' /' [' ' dimension]+]?
operator ::= '+' OR '-' OR '*'
expression ::= size ' ' operator ' ' size
definition ::= unit ' = ' size
Tokens inside a line are separated by single spaces. The division sign / is a token of its own and has a space on both sides, and so does the = of a definition. A floating-point number is written like 63, 1.3, -2.5 or 1E5.
The first line holds the number of new units . The next lines hold one unit definition each. After them comes a line with the integer , and then lines with one expression or one size each.
- Every power is at least 2 and at most 4.
- A unit name consists of English letters only, is shorter than 10 characters, and is case sensitive.
- A newly defined name differs from every SI symbol and from every name defined before it. The right side of a definition uses SI symbols and previously defined units only.
- No unit appears twice inside one size.
- No line is longer than 140 characters.
- Every input value, output value and intermediate value has absolute value at most .
- No computation divides by 0.
Output
For each computation print one line with the result converted to SI base units. If the two sides of a + or a - have different dimensions the answer cannot be computed, so print Incompatible on that line.
Print a line in this format.
- Start with the size in scientific notation with 6 significant digits: one digit, a point, five digits, the letter
e, the sign of the exponent, and the exponent in two digits or more. This is whatprintf("%.5e", x)in C and"%.5e" % xin Python produce, for example1.40000e+03or-2.50000e-03. If the value is zero, print0.00000e+00with no sign. - Then print the base units whose exponent is positive, in the order m, kg, s, A, K, cd.
- If at least one base unit has a negative exponent, print the token
/next, then those units in the same order with the absolute value of their exponents. - Do not print a unit whose exponent is 0, and do not print an exponent equal to 1.
- Separate tokens with a single space, and put no space around
^. - If no unit is left, print the size alone.