結果

問題 No.518 ローマ数字の和
ユーザー beetbeet
提出日時 2019-04-18 17:35:29
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 4 ms / 2,000 ms
コード長 1,170 bytes
コンパイル時間 2,474 ms
コンパイル使用メモリ 213,696 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-22 08:21:23
合計ジャッジ時間 3,400 ms
ジャッジサーバーID
(参考情報)
judge9 / judge15
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using Int = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}

//INSERT ABOVE HERE
string dp[5050];
map<string, int> rev;
int decode(string s){
  assert(rev.count(s));
  return rev[s];  
}

string encode(int x){
  string &res=dp[x];
  if(!res.empty()) return res;
  int a=(x/1000)%10;
  int b=(x/ 100)%10;
  int c=(x/  10)%10;
  int d=(x/   1)%10;
  for(int i=0;i<a;i++) res+="M";
  
  if(b==9) res+="CM",b=0;
  if(b==4) res+="CD",b=0;
  if(b>=5) res+="D",b-=5;
  for(int i=0;i<b;i++) res+="C";  


  if(c==9) res+="XC",c=0;
  if(c==4) res+="XL",c=0;
  if(c>=5) res+="L",c-=5;
  for(int i=0;i<c;i++) res+="X";
  
  if(d==9) res+="IX",d=0;
  if(d==4) res+="IV",d=0;
  if(d>=5) res+="V",d-=5;
  for(int i=0;i<d;i++) res+="I";  
  
  return res;
}

signed main(){
  for(int i=0;i<4000;i++)
    rev[encode(i)]=i;
  
  int n;
  cin>>n;
  int res=0;
  while(n--){
    string s;
    cin>>s;
    res+=decode(s);
  }
  if(res>3999){
    cout<<"ERROR"<<endl;
    return 0;
  }
  cout<<encode(res)<<endl;
  return 0;
}
0