/
089.cpp
208 lines (179 loc) · 6.17 KB
/
089.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
//
// main.cpp
// Prbl089
// Project Euler
//
// Created by Zhiming Wang on 12/11/12.
// Copyright (c) 2012 Zhiming Wang. All rights reserved.
// -----------------------------------------------------
// Algorithm:
//
// Dynamic programming.
//
// First we list all legal numerals and subtractive combinations as seen in
// <code>onedigitStandards</code>, <code>twoDigitStandards</code>, and
// <code>threeDigitStandards</code>. Each <code>Standard</code> object
// contains two fields: the integral value, and the length of representation.
// The first field, the Roman numeral representation, is only for readability.
// 1000 is not included since it will be treated differently.
//
// Then, for 0 <= n <= 999, it is easy to prove that the minimal lengths
// satisfy:
//
// length[0] = 0 (only for convinience of presenting formula);
// length[n] = min{length[n - standard.value] + standard.length,
//
// standard is a d-digit Standard object (d is the number of
// digits in n), where:
// (i) standard.value <= n,
// (ii) standard.value >= standardValue[n - standard.value],
// where standardValue[n] records the maximal standard
// used in the minimal representation of n.
// }.
//
// For n >= 1000, it must contain Floor[n / 1000] Ms (1000), and the rest is
// treated as 0 <= n <= 999.
// -------------------------
// Answer:
//
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
struct Standard {
unsigned value;
unsigned length;
std::string representation;
};
static const std::vector<Standard> ONE_DIGIT_STANDARDS =
{{1, 1, "I"}, {4, 2, "IV"}, {5, 1, "V"}, {9, 2, "IX"}};
static const std::vector<Standard> TWO_DIGIT_STANDARDS =
{{10, 1, "X"}, {40, 2, "XL"}, {50, 1, "L"}, {90, 2, "XC"}};
static const std::vector<Standard> THREE_DIGIT_STANDARDS =
{{100, 1, "C"}, {400, 2, "CD"}, {500, 1, "D"}, {900, 2, "CM"}};
enum ROMAN_NUMERAL {
I = 1,
V = 5,
X = 10,
L = 50,
C = 100,
D = 500,
M = 1000,
};
static void compute0To999(std::vector<unsigned>& lengths);
static inline unsigned computeMinimalLength(unsigned n,
std::vector<unsigned>& lengths);
static unsigned romanToInt(const std::string& romanString);
int main() {
std::vector<unsigned> lengths;
compute0To999(lengths);
std::ifstream input("/Users/ZhimingWang/Dropbox/02ProgrammingProjects/"
"ProjectEuler/C++/Prbl089/roman.txt");
if (!input.is_open()) {
std::cerr << "Error opening roman.txt!" << std::endl;
exit(-1);
}
unsigned originalLengthSum = 0;
unsigned minimalLengthSum = 0;
while (input.good()) {
std::string romanString;
std::getline(input, romanString);
if (romanString.empty()) {
break;
}
originalLengthSum += romanString.length();
unsigned decimalValue = romanToInt(romanString);
minimalLengthSum += computeMinimalLength(decimalValue, lengths);
}
std::cout << originalLengthSum - minimalLengthSum << std::endl;
return 0;
}
static void compute0To999(std::vector<unsigned>& lengths) {
std::vector<unsigned> standardValues;
lengths.clear();
standardValues.clear();
// length[0] = 0
lengths.push_back(0);
standardValues.push_back(0);
for (unsigned n = 1; n <= 999; ++n) {
const std::vector<Standard>* standardsPtr;
if (n < 10) {
standardsPtr = &ONE_DIGIT_STANDARDS;
} else if (n < 100) {
standardsPtr = &TWO_DIGIT_STANDARDS;
} else {
standardsPtr = &THREE_DIGIT_STANDARDS;
}
const std::vector<Standard>& standards = *standardsPtr;
unsigned minLength = n + 1;
unsigned maxStandardValue = 0;
for (std::vector<Standard>::const_iterator
standardsIt = standards.begin();
standardsIt != standards.end();
++standardsIt) {
const Standard& standard = *standardsIt;
// condition (i) for standard
if (standard.value > n) {
break;
}
unsigned reduced = n - standard.value;
if (standard.value < standardValues[reduced]) {
continue;
}
unsigned newLength = lengths[reduced] + standard.length;
if (newLength < minLength) {
minLength = newLength;
maxStandardValue = standard.value;
}
}
lengths.push_back(minLength);
standardValues.push_back(maxStandardValue);
}
}
static unsigned computeMinimalLength(unsigned n,
std::vector<unsigned>& lengths) {
return n / 1000 + lengths[n % 1000];
}
static unsigned romanToInt(const std::string& romanString) {
std::basic_string<ROMAN_NUMERAL> romanValue;
for (std::string::const_iterator i = romanString.begin();
i != romanString.end();
++i) {
switch (*i) {
case 'I' :
romanValue += I;
break;
case 'V' :
romanValue += V;
break;
case 'X' :
romanValue += X;
break;
case 'L' :
romanValue += L;
break;
case 'C' :
romanValue += C;
break;
case 'D' :
romanValue += D;
break;
case 'M' :
romanValue += M;
break;
}
}
int decimalValue = 0;
for (std::basic_string<ROMAN_NUMERAL>::const_iterator
i = romanValue.begin();
i != romanValue.end();
++i) {
if ((i + 1 != romanValue.end()) && (*i < *(i + 1))) {
// Subtractive pair
decimalValue -= *i;
} else {
decimalValue += *i;
}
}
return static_cast<unsigned>(decimalValue);
}