結果

問題 No.518 ローマ数字の和
ユーザー mamekinmamekin
提出日時 2017-06-03 01:38:45
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,523 bytes
コンパイル時間 1,575 ms
コンパイル使用メモリ 104,696 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-22 02:19:42
合計ジャッジ時間 2,602 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#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;
}
0