-
Notifications
You must be signed in to change notification settings - Fork 0
/
00759.cpp
79 lines (59 loc) · 1.94 KB
/
00759.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <bits/stdc++.h>
using namespace std;
#define ll long long
// I=1, V=5, X=10, L=50, C=100, D=500, and M=1000, IV=4, IX=9, XL=40, XC=90, CD=400, CM=900.;
map<char, ll> rom({
{'I', 1}, {'V', 5}, {'X', 10}, {'L', 50}, {'C', 100}, {'D', 500}, {'M', 1000}
});
vector<pair<string, ll>> conv({
{"M", 1000}, {"CM", 900}, {"D", 500}, {"CD", 400}, {"C", 100}, {"XC", 90},
{"L", 50}, {"XL", 40}, {"X", 10}, {"IX", 9}, {"V", 5}, {"IV", 4}, {"I", 1}
});
map<string, ll> mconv({
{"M", 1000}, {"CM", 900}, {"D", 500}, {"CD", 400}, {"C", 100}, {"XC", 90},
{"L", 50}, {"XL", 40}, {"X", 10}, {"IX", 9}, {"V", 5}, {"IV", 4}, {"I", 1}
});
string AtoR(ll res){
string temp;
for(auto &[hi, val]: conv){
// cout << res << " " << val << "\n";
while(res >= val){
temp += hi;
res -= val;
}
}
return temp;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
string str;
while(getline(cin, str)){
if(str == ""){
cout << 0 << "\n";
continue;
}
ll res = 0; ll MaxS = LLONG_MIN;
bool not_valid = false;
for(ll i = 0; str[i]; i++){
if(str[i+1] && (rom[str[i]] < rom[str[i+1]])){
string temp = string(1, str[i]) + string(1, str[i+1]);
if(mconv.find(temp) == mconv.end()) not_valid = true;
res += rom[str[i+1]] - rom[str[i]];
i++;
}
else res += rom[str[i]];
}
ll streak = 1;
for(ll i = 1; str[i]; i++){
if(str[i] == str[i-1]) ++streak;
else streak = 1;
MaxS = max(MaxS, streak);
}
if(not_valid||MaxS>=4|| str != AtoR(res)) cout << "This is not a valid number\n";
else cout << res << "\n";
}
return 0;
}