結果
| 問題 | 
                            No.518 ローマ数字の和
                             | 
                    
| コンテスト | |
| ユーザー | 
                             beet
                         | 
                    
| 提出日時 | 2019-04-18 17:35:29 | 
| 言語 | C++17  (gcc 13.3.0 + boost 1.87.0)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 5 ms / 2,000 ms | 
| コード長 | 1,170 bytes | 
| コンパイル時間 | 2,886 ms | 
| コンパイル使用メモリ | 205,336 KB | 
| 最終ジャッジ日時 | 2025-01-07 02:28:10 | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge1 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 19 | 
ソースコード
#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;
}
            
            
            
        
            
beet