Under construction

Still being built. Things may change

Billing Tables

Time limit1sMemory limit128 MB

Summary
Given an ordered old billing table with range-based prefix rules, build the minimal prefix-only dictionary table (no prefix a prefix of another) that reproduces exactly the same plan decisions for all 11-digit numbers.
Level

Hard8 of 10

Topics
Trie, Greedy, Recursion, Simulation
Solved
No attempts yet

Problem

In telecommunications, calls to different phone numbers may be charged under different rates or billing plans. A phone carrier keeps a billing table that decides which plan applies to each call.

Every phone number has exactly 11 digits. The old billing table has n lines. Each line describes a range of number prefixes written as A - B, followed by a billing plan name. The line matches every phone number whose leading digits fall inside that range. For example, the line 7919 - 921 matches every phone number that begins with 7919, 7920, or 7921.

To find the plan for a call, the table is read from top to bottom and the first matching line decides the plan. If no line matches, the number is invalid and needs no plan. A line may use the special plan name invalid as another way to mark numbers as invalid. The same plan name may appear on several lines that describe very different numbers.

The old table may contain redundant entries, so the carrier wants a cleaner table. The new table is a list of plain prefixes (no ranges), each with a plan name, in which no prefix is a prefix of another prefix in the table. A single dictionary lookup then gives the plan of any phone number. The plan invalid must not appear in the new table — invalid numbers are exactly the numbers with no matching prefix. Among all tables that reproduce the old table's decisions exactly, report one with the fewest lines.

Input

The first line contains one integer n (1 ≤ n ≤ 100) — the number of lines in the old billing table.

Each of the next n lines describes one rule as four space-separated tokens: prefix A, a minus sign -, prefix B, and the billing plan name. Each prefix has 1 to 11 digits and each plan name has 1 to 20 lowercase letters.

Write |A| and |B| for the number of digits in A and B. The prefixes satisfy 1 ≤ |B| ≤ |A| ≤ 11, and the last |B| digits of A, read as a string, are lexicographically less than or equal to B. The rule matches every phone number whose first |A| − |B| digits equal the first |A| − |B| digits of A and whose next |B| digits lie between the last |B| digits of A and B, inclusive.

Output

On the first line print one integer k — the minimum number of lines the new table needs to describe the old table exactly.

Then print k lines giving the new table in lexicographic order of the prefixes. Each line has two space-separated tokens: the prefix (at least one digit) and its billing plan name.

If every phone number is invalid, print just the single number 0.

Examples4

  1. Example 1

    Input
    8
    7919 - 921 cell
    7921800 - 999 priv
    1 - 1 usa
    760 - 9 rsv
    7928 - 29 rsv
    7600 - 7899 spec
    73 - 77 invalid
    7 - 7 cis
    
    Expected output
    35
    1 usa
    70 cis
    71 cis
    72 cis
    76 rsv
    77 spec
    78 spec
    790 cis
    7910 cis
    7911 cis
    7912 cis
    7913 cis
    7914 cis
    7915 cis
    7916 cis
    7917 cis
    7918 cis
    7919 cell
    7920 cell
    7921 cell
    7922 cis
    7923 cis
    7924 cis
    7925 cis
    7926 cis
    7927 cis
    7928 rsv
    7929 rsv
    793 cis
    794 cis
    795 cis
    796 cis
    797 cis
    798 cis
    799 cis
    
  2. Example 2

    Input
    1
    5 - 5 abc
    
    Expected output
    1
    5 abc
    
  3. Example 3

    Input
    2
    12 - 12 a
    1 - 1 b
    
    Expected output
    10
    10 b
    11 b
    12 a
    13 b
    14 b
    15 b
    16 b
    17 b
    18 b
    19 b
    
  4. Example 4

    Input
    1
    1234 - 236 p
    
    Expected output
    3
    1234 p
    1235 p
    1236 p