This page is still under construction.

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

Geocaching Coordinates

Time limit1sMemory limit1024 MB

Summary
Given a coordinate formula with lowercase letter slots and rules for each variable's allowed values, print every distinct resulting coordinate in lexicographic order.
Level

Medium5 of 10

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

Problem

In geocaching, the coordinates of a hidden treasure are sometimes given as a formula in which some of the digits have been replaced by letters, for example N58 24.1ab E26 44.3c. Each lowercase Latin letter is a variable that stands for a single number.

Given the formula and the values each variable may take, write a program that prints every coordinate that can be obtained by substituting the variables.

Input

The first line contains the formula. It consists of a latitude and a longitude, each written as a compass letter (N, S, E, W) followed by degrees, minutes, and the fractional part of the minutes, in the format shown in the examples. Lowercase Latin letters in the formula mark variable slots. The formula is at most 25 characters long.

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 in the form name=list. A variable name is always a single lowercase Latin letter. The list is comma-separated, and each element is either a single value or a range lo..hi. Every value and range endpoint is a one- or two-digit integer, or the name of a previously described variable.

When an element is a variable name, it refers to the value assigned to that variable in the current combination, so a later variable may depend on an earlier one. If the same letter appears several times in the formula, it always takes the same value within one combination. You may assume that no variable's description ever produces duplicate values.

Output

Print every distinct coordinate that can be obtained by substituting the variables with their allowed values. Print the coordinates in ascending lexicographic (dictionary) order, one per line.

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.134 E26 44.319
    N58 24.144 E26 44.319
    
  2. Example 2

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