Generalized Roman Numerals
Time limit3sMemory limit256 MB
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 , 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 . In general, if two expressions and have values and , then when , and 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 is the number of the test case counting from 1, and are the distinct values the string can take, listed in increasing order. Separate the values with single spaces.