Geopeitus

Time limit1sMemory limit1024 MB

Summary
Expand each variable's value list, substitute every combination into the coordinate formula, and print each resulting coordinate in the specified digit order.
Level

Medium4 of 10

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

Problem

In geocaching, the coordinates of a cache are sometimes given as a formula in which some digits have been replaced by letters, for example N58 24.1ab E26 44.3c.

Write a program that outputs every coordinate corresponding to the given formula together with the possible values of the variables.

Input

The first line contains the formula. It consists of a latitude representation and a longitude representation; each representation is made up of a compass marker (N, S, E, W) followed by readings for degrees, minutes, and the fractional part of a minute. The length of the formula does not exceed 25 characters.

The second line contains the number of variables NN (1≤N≤31 \le N \le 3). Each of the next NN lines describes one variable: the variable name, an equals sign (=), and a comma-separated list whose elements are either single values or value ranges. A range is written as lo..hi and is inclusive of both endpoints (for example, 2..4 means 2, 3, 4). A variable name is always a single lowercase Latin letter. Values and range endpoints may be one- or two-digit integers. You may assume that no variable's description produces duplicate values.

Each occurrence of a variable's letter inside the formula is replaced textually by the decimal notation of the chosen value, so a two-digit value inserts two characters.

Output

Output every coordinate that can be obtained by substituting the variables with their allowed values, one coordinate per line.

Because the output is compared byte-for-byte, print the coordinates in the following fixed order. Let the variables, in the order they appear in the input, be v1,v2,…,vNv_1, v_2, \ldots, v_N. Enumerate all combinations so that the first variable v1v_1 changes fastest (on every consecutive line) and the last variable vNv_N changes slowest; equivalently, iterate vNv_N in the outermost loop and v1v_1 in the innermost loop. Within a single variable, take its values in the order they are listed, expanding each range from its lower endpoint to its upper endpoint.

Examples2

  1. Example 1

    Input
    N58 24.1a4 E26 44.319
    1
    a=2,4
    
    Expected output
    N58 24.124 E26 44.319
    N58 24.144 E26 44.319
    
  2. Example 2

    Input
    N58 24.1ab E26 44.3c
    3
    a=2..4
    b=4,8
    c=19
    
    Expected output
    N58 24.124 E26 44.319
    N58 24.134 E26 44.319
    N58 24.144 E26 44.319
    N58 24.128 E26 44.319
    N58 24.138 E26 44.319
    N58 24.148 E26 44.319