結果
問題 | No.518 ローマ数字の和 |
ユーザー |
|
提出日時 | 2017-05-28 22:24:50 |
言語 | Python2 (2.7.18) |
結果 |
AC
|
実行時間 | 14 ms / 2,000 ms |
コード長 | 1,279 bytes |
コンパイル時間 | 58 ms |
コンパイル使用メモリ | 6,940 KB |
実行使用メモリ | 6,912 KB |
最終ジャッジ日時 | 2024-09-21 15:35:15 |
合計ジャッジ時間 | 1,314 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 19 |
ソースコード
#!/usr/bin/python2# -*- coding: utf-8 -*-# †from collections import namedtuplefrom itertools import groupbyT = namedtuple('T', 'num, cnt')v = ['IV', 'XL', 'CD', 'M-']num_letter = {10**i * [1,5][j]: ch for i, s in enumerate(v) for j, ch in enumerate(s)}letter_num = {v: k for k, v in num_letter.items()}def create(n):arr = []d = 0while n > 0:x = n % 10if x == 9:arr.append(v[d][0] + v[d+1][0])elif x == 4:arr.append(v[d])else:p, q = divmod(x, 5)arr.append(v[d][1] * p + v[d][0] * q)n //= 10d += 1arr = [s.replace('M-', 'MMMM') for s in arr]return ''.join(reversed(arr))def parse(s):grp = [T(k, len(list(g))) for k, g in groupby(letter_num[ch] for ch in s)]n = len(grp)i = 0res = 0while i < n:if i+1 < n and grp[i].num < grp[i+1].num:assert grp[i].cnt == 1 and grp[i+1].cnt == 1res += grp[i+1].num - grp[i].numi += 1else:res += grp[i].num * grp[i].cnti += 1return res###n = int(raw_input())line = raw_input()summ = sum(map(parse, line.split()))if summ > 3999:print 'ERROR'exit(0)res = create(summ)print res