QOJ.ac

QOJ

Limite de temps : 1 s Limite de mémoire : 1024 MB Points totaux : 100

#8443. 转换罗马数字

Statistiques

罗马数字是一种起源于古罗马的数字系统,在中世纪晚期之前在整个欧洲被广泛使用。它与我们今天主要使用的阿拉伯数字系统不同,它是通过拉丁字母的组合来书写数字的,其中每个字母都被赋予一个固定的整数值。多年来,人们使用了许多不同的字母,这导致了频繁的混淆。但“现代”风格仅使用以下七个字母:

I V X L C D M
1 5 10 50 100 500 1000

卡蒂萨克号(Cutty Sark)船尾的罗马数字,显示吃水深度(英尺)。数字从下到上范围为 13 到 22。(CC BY-SA 3.0)

关于罗马数字的一个非常普遍的误解是它们使用通用的减法语法。在减法语法中,写在较大数字前面的较小数字将从较大数字中减去。4 可以写成 IIII 或 IV ($5 - 1$)。罗马人自己实际上只在较小的数字中使用减法语法。对于较大的数字,为了更清晰起见,通常会避免使用这种语法。例如,499 可以写成 LDVLIV、XDIX、VDIV 或 ID,而较古老的来源很可能会使用 CDXCIX。罗马数字是不是很有趣?

给定 $n$ 个用罗马数字系统书写的数字,你的任务是将它们转换为常规的阿拉伯数字系统。

在本题中,我们将减去任何写在较大数字左侧的数字,即使它们并不直接相邻。

输入格式

输入以一个整数 $0 < n \le 10^3$ 开始。接下来是 $n$ 行,每行包含最多 $3 \cdot 10^5$ 个罗马数字字符。输入中罗马数字字符的总数最多为 $3 \cdot 10^5$。

输出格式

输出对应的阿拉伯数字,每行一个数字。

样例

样例输入 1

5
CDXCIX
ID
IV
IIIIV
IIIIIIV

样例输出 1

499
499
4
1
-1

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.