This page is still under construction.

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

Generalized Roman Numerals

Time limit3sMemory limit256 MB

Summary
Given a string of Roman letters, list every distinct value it can take under all parenthesizations of the subtract-when-smaller rule.
Level

Medium7 of 10

Topics
Dynamic programming, Intervals, Recursion
Solved
No attempts yet

Problem

The Romans wrote numbers with the letters I, V, X, L and C, worth 1, 5, 10, 50 and 100. XXXVII is 10+10+10+5+1+110+10+10+5+1+1, that is 37. They usually wrote the letters in non-increasing order, but a single smaller letter placed in front of a larger one is subtracted from it, so IV is 4, IX is 9, XL is 40 and XC is 90. They wrote 94 as XCIV.

VIC is not a traditional Roman numeral, but you can read it as another way of writing 94: VI is 6, so VIC is 100−6100-6. In general, if two expressions aa and bb have values v(a)v(a) and v(b)v(b), then v(ab)=v(a)+v(b)v(ab) = v(a) + v(b) when v(a)≥v(b)v(a) \ge v(b), and v(ab)=v(b)−v(a)v(ab) = v(b) - v(a) otherwise.

This rule is ambiguous, because a different order of evaluation can give a different value. Take IVX. IV is 4 and X is 10, so that order makes IVX 6. But I is 1 and VX is 5, so the other order makes IVX 4. To fix an order you can add parentheses. Given a string of these letters, find every value it can take.

Input

The input holds several test cases. Each line holds one string made only of the letters I, V, X, L and C, of length 1 to 50. A line holding a single 0 ends the input.

Output

For each test case print one line of the form Case i: v1 v2 .... Here ii is the number of the test case counting from 1, and v1,v2,…v_1, v_2, \dots are the distinct values the string can take, listed in increasing order. Separate the values with single spaces.

Examples3

  1. Example 1

    Input
    IVX
    XIXIX
    0
    
    Expected output
    Case 1: 4 6
    Case 2: 8 10 28 30 32
    
  2. Example 2

    Input
    I
    V
    X
    L
    C
    0
    
    Expected output
    Case 1: 1
    Case 2: 5
    Case 3: 10
    Case 4: 50
    Case 5: 100
    
  3. Example 3

    Input
    XXXVII
    IV
    IX
    XL
    XC
    XCIV
    VIC
    0
    
    Expected output
    Case 1: 17 37
    Case 2: 4
    Case 3: 9
    Case 4: 40
    Case 5: 90
    Case 6: 94 96
    Case 7: 94