結果
| 問題 |
No.518 ローマ数字の和
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-02-04 04:02:26 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 2,000 ms |
| コード長 | 956 bytes |
| コンパイル時間 | 1,862 ms |
| コンパイル使用メモリ | 197,980 KB |
| 最終ジャッジ日時 | 2025-01-18 11:11:57 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 19 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int i=0; i<(int)(n); i++)
vector<int> x = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
vector<string> y = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
string encode(int n) {
string s;
for (int i = 0; i < x.size(); i++) {
while (n >= x[i]) {
n -= x[i];
s += y[i];
}
}
return s;
}
int decode(string s) {
int ret = 0;
int p = 0;
for (int i = 0; i < x.size(); i++) {
while (s.substr(p, y[i].size()) == y[i]) {
ret += x[i];
p += y[i].size();
if (p == s.length())
break;
}
}
return ret;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
int ret = 0;
for (int i = 0; i < n; i++) {
string r;
cin >> r;
ret += decode(r);
}
if (ret >= 4000)
cout << "ERROR" << endl;
else
cout << encode(ret) << endl;
return 0;
}