-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathstandard_checker_interactor.cpp
More file actions
213 lines (185 loc) · 4.9 KB
/
standard_checker_interactor.cpp
File metadata and controls
213 lines (185 loc) · 4.9 KB
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
209
210
211
212
213
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <regex.h>
#include <stdexcept>
#include <string>
#include <vector>
namespace CheckerCodes {
constexpr int AC = 0;
constexpr int WA = 1;
constexpr int PARTIAL = 7;
} // namespace CheckerCodes
void assertWA(bool);
// Implementation of the tricky whitespace logic for standard checkers.
namespace standard_whitespace_detail {
enum WhitespaceFlag { NONE = 0, SPACE = 1, NEWLINE = 2, ALL = 3 };
WhitespaceFlag current_flag = ALL; // At checker start, consume all whitespace.
void pokeFlag(WhitespaceFlag flag) {
if (current_flag != NONE && (current_flag != NEWLINE || flag != ALL)) {
throw std::runtime_error("Never call two whitespace methods in a row, "
"except for readNewLine() followed by readEOF().");
}
current_flag = flag;
}
enum ConsumeResult {
NO_WHITESPACE,
NO_LINES,
LINES,
};
ConsumeResult consumeWhitespace() {
int c = std::cin.get();
ConsumeResult result = NO_WHITESPACE;
while (isspace(c) && c != EOF) {
if (result == NO_WHITESPACE) {
result = NO_LINES;
}
if (c == '\r' || c == '\n') {
result = LINES;
}
c = std::cin.get();
}
std::cin.unget();
current_flag = NONE;
return result;
}
void preReadToken() {
switch (current_flag) {
case NONE:
throw std::runtime_error(
"Must not call readInt (or readToken, or readFloat) twice in a row!");
case SPACE:
assertWA(consumeWhitespace() == NO_LINES);
break;
case NEWLINE:
assertWA(consumeWhitespace() == LINES);
break;
case ALL:
consumeWhitespace();
break;
}
}
} // namespace standard_whitespace_detail
namespace regex_helpers {
regex_t compile(const char *pattern) {
regex_t re;
if (regcomp(&re, pattern, REG_EXTENDED | REG_NOSUB) != 0) {
throw std::runtime_error("Pattern failed to compile.");
}
return re;
}
bool match(regex_t re, const std::string &text) {
return regexec(&re, text.c_str(), 0, NULL, 0) == 0;
}
} // namespace regex_helpers
void errorHook();
void exitWA() {
errorHook();
std::exit(CheckerCodes::WA);
}
void assertWA(bool condition) {
if (!condition) {
exitWA();
}
}
void readSpace() {
standard_whitespace_detail::pokeFlag(standard_whitespace_detail::SPACE);
}
void readNewLine() {
standard_whitespace_detail::pokeFlag(standard_whitespace_detail::NEWLINE);
}
void readEOF() {
standard_whitespace_detail::pokeFlag(standard_whitespace_detail::ALL);
standard_whitespace_detail::consumeWhitespace();
assertWA(std::cin.get() == EOF);
}
std::string readToken(char min_char = 0, char max_char = 127) {
standard_whitespace_detail::preReadToken();
static constexpr size_t MAX_TOKEN_SIZE = 1e7;
std::string token;
int c = std::cin.get();
assertWA(!isspace(c));
while (!isspace(c) && c != EOF) {
assertWA(token.size() < MAX_TOKEN_SIZE);
assertWA(min_char <= c && c <= max_char);
token.push_back(char(c));
c = std::cin.get();
}
std::cin.unget();
return token;
}
bool validateInt(const std::string &token) {
if (token == "0") {
return true;
}
auto i = 0u;
if (i < token.size() && token[i] == '-') {
++i;
}
if (i >= token.size() || token[i] < '1' || '9' < token[i]) {
return false;
}
for (i++; i < token.size(); i++) {
if (token[i] < '0' || '9' < token[i]) {
return false;
}
}
return true;
}
long long readInt(long long lo, long long hi) {
std::string token = readToken();
assertWA(validateInt(token));
long long parsedInt;
try {
parsedInt = stoll(token);
} catch (const std::invalid_argument &) {
exitWA();
} catch (const std::out_of_range &) {
exitWA();
}
assertWA(lo <= parsedInt && parsedInt <= hi);
return parsedInt;
}
long double readFloat(long double min, long double max,
long double eps = 1e-9) {
static regex_t re = regex_helpers::compile("^-?(0|[1-9][0-9])(\\.[0-9]+)?$");
std::string token = readToken();
assertWA(regex_helpers::match(re, token));
long double parsedDouble;
try {
parsedDouble = stold(token);
} catch (const std::invalid_argument &) {
exitWA();
} catch (const std::out_of_range &) {
exitWA();
}
assertWA(min - eps <= parsedDouble && parsedDouble <= max + eps);
return parsedDouble;
}
template <typename T>
std::vector<T> readIntArray(size_t N, long long lo, long long hi) {
std::vector<T> arr;
arr.reserve(N);
for (size_t i = 0; i < N; i++) {
if (i) {
readSpace();
}
arr.push_back(readInt(lo, hi));
}
readNewLine();
return arr;
}
void errorHook() {}
// If this is a checker:
// int main(int argc, char **argv) {
// std::ifstream judge_input(argv[1]);
// std::ifstream submission_output(argv[2]);
// std::cin.rdbuf(submission_output.rdbuf());
// std::ifstream judge_answer(argv[3]);
// }
// If this is an interactor:
// int main(int argc, char **argv) {
// std::ifstream judge_input(argv[1]);
// std::ifstream judge_answer(argv[2]);
// }