結果
| 問題 |
No.518 ローマ数字の和
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2017-06-03 01:38:45 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 2,000 ms |
| コード長 | 2,523 bytes |
| コンパイル時間 | 1,065 ms |
| コンパイル使用メモリ | 104,540 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-09-22 03:58:20 |
| 合計ジャッジ時間 | 1,993 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 19 |
ソースコード
#define _USE_MATH_DEFINES
#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
#include <iterator>
using namespace std;
class romanNumerals
{
private:
static const string ROMAN_CHAR;
public:
static string toStr(int a){
if(!(0 < a && a < 4000))
return "";
string ans;
for(int i=0; i<4; ++i){
int x = a % 10;
a /= 10;
if(x == 9){
ans += ROMAN_CHAR[2*i+2];
ans += ROMAN_CHAR[2*i];
}
else if(x == 4){
ans += ROMAN_CHAR[2*i+1];
ans += ROMAN_CHAR[2*i];
}
else{
ans += string(x % 5, ROMAN_CHAR[2*i]);
ans += string(x / 5, ROMAN_CHAR[2*i+1]);
}
}
reverse(ans.begin(), ans.end());
return ans;
}
static int toInt(const string& s){
int n = s.size();
int k = 0;
int ans = 0;
for(int i=3; i>=0; --i){
ans *= 10;
if(k < n - 1 && s[k] == ROMAN_CHAR[2*i] && s[k+1] == ROMAN_CHAR[2*i+2]){
ans += 9;
k += 2;
}
else if(k < n - 1 && s[k] == ROMAN_CHAR[2*i] && s[k+1] == ROMAN_CHAR[2*i+1]){
ans += 4;
k += 2;
}
else{
if(k < n && s[k] == ROMAN_CHAR[2*i+1]){
ans += 5;
++ k;
}
int len = 0;
while(k < n && s[k] == ROMAN_CHAR[2*i]){
++ ans;
++ k;
++ len;
}
if(len > 3)
return 0;
}
}
if(k < n)
return 0;
else
return ans;
}
};
const string romanNumerals::ROMAN_CHAR = "IVXLCDM__";
int main()
{
int n;
cin >> n;
int sum = 0;
for(int i=0; i<n; ++i){
string s;
cin >> s;
sum += romanNumerals::toInt(s);
}
string ans = romanNumerals::toStr(sum);
if(ans.empty())
cout << "ERROR" << endl;
else
cout << ans << endl;
return 0;
}