-
Notifications
You must be signed in to change notification settings - Fork 0
/
89.cpp
88 lines (73 loc) · 1.4 KB
/
89.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
80
81
82
83
84
#include <iostream>
#include <fstream>
#include <vector>
#include <unordered_map>
#include "utils.h"
using Roman = std::string;
const std::vector<Roman> roman_numerals = {
"M", "CM", "D", "CD", "C", "XC",
"L", "XL", "X", "IX", "V", "IV", "I"
};
const std::unordered_map<Roman, int> rtoi_map = {
{"I", 1},
{"IV", 4},
{"V", 5},
{"IX", 9},
{"X", 10},
{"XL", 40},
{"L", 50},
{"XC", 90},
{"C", 100},
{"CD", 400},
{"D", 500},
{"CM", 900},
{"M", 1000}
};
std::vector<Roman> read_file() {
std::ifstream f("89.txt");
std::vector<Roman> romans;
Roman line;
while (std::getline(f, line)) romans.push_back(std::move(line));
return romans;
}
int rtoi(Roman r) {
int i = 0;
int res = 0;
while (i < r.size()) {
bool matched = false;
if (i + 1 < r.size()) {
Roman cand = r.substr(i, 2);
if (rtoi_map.find(cand) != rtoi_map.end()) {
matched = true;
res += rtoi_map.at(cand);
i += 2;
}
}
if (!matched) {
res += rtoi_map.at(r.substr(i, 1));
++i;
}
}
return res;
}
Roman itor(int i) {
int k = 0;
Roman s;
for (auto &r : roman_numerals) {
int val = rtoi_map.at(r);
if (i < val) continue;
k = i / val;
for (int i = 0; i < k; ++i) s += r;
i %= val;
}
return s;
}
int main() {
auto romans = read_file();
int ans = 0;
for (auto &roman : romans) {
auto shortened = itor(rtoi(roman));
ans += roman.size() - shortened.size();
}
std::cout << ans << std::endl;
}