This page is still under construction.

Parts of this page are still being built. What you see may change.

Exchange Rates

Interview

Time limit1sMemory limit128 MB

Summary
Maintain exchange rates between named items as assertions arrive, then answer each query with the rate in lowest terms or question marks if unknown.
Level

Medium6 of 10

Topics
Graph, Union-find, Math, Number theory
Solved
No attempts yet

Problem

Using money to pay for goods and services usually makes life easier, but sometimes people prefer to trade items directly without any money changing hands. To keep a consistent "price", traders set an exchange rate between items.

The exchange rate between two items A and B is written as two positive integers mm and nn, meaning that mm of item A is worth nn of item B. For example, 2 stoves might be worth 3 refrigerators. (Mathematically 1 stove is worth 1.5 refrigerators, but since half a refrigerator is hard to come by, exchange rates are always written with integers.)

Given a list of exchange rates, write a program that computes the exchange rate between any two items.

Input

The input consists of one or more commands, followed by a line beginning with a period (.) that marks the end of the input. Each command is on a line by itself and is either an assertion or a query.

An assertion begins with an exclamation point (!) and has the form

! m itema = n itemb

where itema and itemb are distinct item names and mm and nn are both positive integers less than 100. It states that mm of itema are worth nn of itemb.

A query begins with a question mark (?) and has the form

? itema = itemb

and asks for the exchange rate between itema and itemb. Here itema and itemb are distinct items that have both appeared in previous assertions (not necessarily the same assertion).

Output

For each query, output the exchange rate between itema and itemb based on every assertion made up to that point. The rate must consist of integers and must be reduced to lowest terms. The output has the form

m itema = n itemb

If the exchange rate cannot be determined at that point, use question marks instead of the integers:

? itema = ? itemb

Constraints

  • Item names are at most 20 characters long and consist only of lowercase letters.
  • Only the singular form of an item name is used (no plurals).
  • There are at most 60 distinct items.
  • There is at most one assertion for any pair of distinct items.
  • No contradictory assertions are given. For example, "2 pig = 1 cow", "2 cow = 1 horse", and "2 horse = 3 pig" are contradictory.
  • Assertions are not necessarily in lowest terms, but the output must be.
  • Although assertions use numbers less than 100, a query may produce larger numbers; when reduced to lowest terms they never exceed 10000.

Examples3

  1. Example 1

    Input
    ! 6 shirt = 15 sock
    ! 47 underwear = 9 pant
    ? sock = shirt
    ? shirt = pant
    ! 2 sock = 1 underwear
    ? pant = shirt
    .
    
    Expected output
    5 sock = 2 shirt
    ? shirt = ? pant
    45 pant = 188 shirt
    
  2. Example 2

    Input
    ! 2 stove = 3 fridge
    ? stove = fridge
    ? fridge = stove
    .
    
    Expected output
    2 stove = 3 fridge
    3 fridge = 2 stove
    
  3. Example 3

    Input
    ! 4 apple = 6 banana
    ? apple = banana
    .
    
    Expected output
    2 apple = 3 banana