#K55722. Roman Numerals to Integer Conversion
Roman Numerals to Integer Conversion
Roman Numerals to Integer Conversion
Given a string representing a Roman numeral, your task is to convert it to its integer representation. The Roman numeral will be a valid numeral in the range from 1 to 3999.
The numbers are represented by the following symbols:
- I : 1
- V : 5
- X : 10
- L : 50
- C : 100
- D : 500
- M : 1000
Typically, Roman numerals are written largest to smallest from left to right. However, if a smaller numeral appears before a larger numeral, it is subtracted. In mathematical terms, for any two symbols s_i and s_{i+1}, if value(si) < value(si+1), then it contributes -value(si); otherwise, it contributes +value(si). This rule can be expressed in LaTeX as:
$$\text{result} = \sum_{i=1}^{n} \begin{cases} value(s_i) & \text{if} \; i=n \; \text{or} \; value(s_i) \geq value(s_{i+1}) \\- value(s_i) & \text{if} \; value(s_i) < value(s_{i+1}) \end{cases}$$
Your program should read the Roman numeral from standard input and output the corresponding integer to standard output.
inputFormat
The input consists of a single line containing a valid Roman numeral string s.
You should read the input from standard input (stdin).
outputFormat
Output a single integer, which is the numerical value of the Roman numeral.
The output should be written to standard output (stdout).
## sampleIII
3