結果

問題 No.518 ローマ数字の和
ユーザー CELICACELICA
提出日時 2020-09-24 21:49:52
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,748 bytes
コンパイル時間 2,345 ms
コンパイル使用メモリ 188,560 KB
実行使用メモリ 4,500 KB
最終ジャッジ日時 2023-09-10 13:57:58
合計ジャッジ時間 3,487 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 2 ms
4,500 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ul = unsigned long;
using ull = unsigned long long;

string encode(string s)
{
	vector<string> vs = { "IV","IX","XL","XC","CD","CM" };
	vector<string> vd = { "IIII","VIIII","XXXX","XXXXL","CCCC","CCCCD" };

	string ss{ s };
	for (unsigned i = 0; i < vs.size(); ++i)
	{
		size_t pos = 0;
		while ((pos = ss.find(vs[i], pos)) != string::npos)
		{
			ss.replace(pos, vs[i].length(), vd[i]);
			pos += vd[i].length();
		}
	}
	return ss;
}

string decode(string s)
{
	vector<string> vs = { "CM","CD","XC","XL","IX","IV" };
	vector<string> vd = { "CCCCD","CCCC","XXXXL","XXXX","VIIII","IIII" };

	string ss{ s };
	for (unsigned i = 0; i < vs.size(); ++i)
	{
		size_t pos = 0;
		while ((pos = ss.find(vd[i], pos)) != string::npos)
		{
			ss.replace(pos, vd[i].length(), vs[i]);
			pos += vs[i].length();
		}
	}
	return ss;
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	int n;
	cin >> n;

	vector<string> r(n);
	for (auto&& it : r)
	{
		string s;
		cin >> s;
		it = encode(s);
	}

	vector<int> v = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 };
	map<char, int> me = { {'I',1},{'V',5},{'X',10},{'L',50},{'C',100},{'D',500},{'M',1000} };
	map<int, string> md = { {1,"I"},{4,"IV"},{5,"V"},{9,"IX"},{10,"X"},{40,"XL"},{50,"L"},{90,"XC"},{100,"C"},{400,"CD"},{500,"D"},{900,"CM"},{1000,"M"} };

	int sum{ 0 };
	for (const auto& it : r)
	{
		for (const auto& it2 : it)
			sum += me[it2];
	}

	string res;
	if (sum > 3999)
		res = "ERROR";
	else
	{
		for (const auto& it : v)
		{
			int d = sum / it;
			if (d)
			{
				for (int i = 0; i < d; ++i)
					res += md[it];
				sum -= it * d;
			}
		}
		res = decode(res);
	}
	cout << res << "\n";

	return 0;
}
0