-
Notifications
You must be signed in to change notification settings - Fork 1
/
126.cc
266 lines (237 loc) · 7.27 KB
/
126.cc
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cstdlib>
#include <iostream>
#include <iterator>
#include <map>
#include <string>
#include <vector>
enum {
#ifdef ONLINE_JUDGE
DEBUG = 0,
#else
DEBUG = 1,
#endif
};
struct Term {
Term(int coef_, int xexp_, int yexp_);
int coef, xexp, yexp;
};
typedef std::vector<Term> Terms;
class Polynomial {
public:
explicit Polynomial(const std::string& expr);
explicit Polynomial(const Terms& terms);
Terms::const_iterator begin() const;
Terms::const_iterator end() const;
private:
Terms terms_;
};
bool operator<(const Term& lhs, const Term& rhs);
Terms ParseTerms(const std::string& expr);
void ParseInt(const std::string& str, int default_value, int* out);
std::ostream& operator<<(std::ostream& out, const Term& term);
std::ostream& operator<<(std::ostream& out, const Polynomial& polynomial);
Polynomial operator*(const Polynomial& lhs, const Polynomial& rhs);
void WriteChar(char c, std::string* str, std::string* co_str);
void WriteString(const std::string& v, std::string* str, std::string* co_str);
void WriteInt(int n, std::string* str, std::string* co_str);
int main() {
for (std::string str0, str1; std::cin >> str0 >> str1 && str0 != "#\n";) {
Polynomial polynomial0(str0);
Polynomial polynomial1(str1);
Polynomial polynomial = polynomial0 * polynomial1;
if (DEBUG) {
std::cerr << "p0 =" << polynomial0 << std::endl;
std::cerr << "p1 =" << polynomial1 << std::endl;
std::cerr << "p0 * p1 =" << polynomial << std::endl;
}
std::string base_line, exp_line;
bool first = true;
for (const Term& term : polynomial) {
assert(term.coef != 0);
if (first) {
if (term.xexp == 0 && term.yexp == 0) {
WriteInt(term.coef, &base_line, &exp_line);
} else if (term.coef == -1) {
WriteChar('-', &base_line, &exp_line);
} else if (term.coef < -1 || 1 < term.coef) {
WriteInt(term.coef, &base_line, &exp_line);
} else {
assert(term.coef == 1);
}
} else {
WriteChar(' ', &base_line, &exp_line);
WriteChar(term.coef < 0 ? '-' : '+', &base_line, &exp_line);
WriteChar(' ', &base_line, &exp_line);
int abs_coef = abs(term.coef);
if (term.xexp == 0 && term.yexp == 0) {
WriteInt(abs_coef, &base_line, &exp_line);
} else if (abs_coef > 1) {
WriteInt(abs_coef, &base_line, &exp_line);
} else {
assert(abs_coef == 1);
}
}
first = false;
if (term.xexp >= 1) {
WriteChar('x', &base_line, &exp_line);
}
if (term.xexp >= 2) {
WriteInt(term.xexp, &exp_line, &base_line);
}
if (term.yexp >= 1) {
WriteChar('y', &base_line, &exp_line);
}
if (term.yexp >= 2) {
WriteInt(term.yexp, &exp_line, &base_line);
}
}
std::cout << exp_line << std::endl;
std::cout << base_line << std::endl;
}
return 0;
}
void WriteChar(char c, std::string* str, std::string* co_str) {
str->push_back(c);
co_str->push_back(' ');
}
void WriteString(const std::string& v, std::string* str, std::string* co_str) {
for (char c : v) {
WriteChar(c, str, co_str);
}
}
void WriteInt(int n, std::string* str, std::string* co_str) {
WriteString(std::to_string(n), str, co_str);
}
Term::Term(int coef_, int xexp_, int yexp_)
: coef(coef_), xexp(xexp_), yexp(yexp_) {}
bool operator<(const Term& lhs, const Term& rhs) {
if (lhs.xexp != rhs.xexp) {
return lhs.xexp > rhs.xexp;
} else {
return lhs.yexp < rhs.yexp;
}
}
Terms ParseTerms(const std::string& expr) {
Terms terms;
enum State { STATE_BEGIN, STATE_COEF, STATE_EXP, } state = STATE_BEGIN;
int coef = 1, xexp = 0, yexp = 0;
char exp = '\0';
std::string buffer;
for (auto i = expr.cbegin(), j = i;; ++i) {
if (i == expr.cend() || *i == '\n') {
if (state == STATE_COEF) {
ParseInt(buffer.assign(j, i), 1, &coef);
terms.push_back(Term(coef, xexp, yexp));
} else {
assert(state == STATE_EXP);
ParseInt(buffer.assign(j, i), 1, exp == 'x' ? &xexp : &yexp);
terms.push_back(Term(coef, xexp, yexp));
}
break;
}
assert(!isspace(*i));
switch (state) {
case STATE_BEGIN:
coef = 1;
xexp = 0;
yexp = 0;
if (*i == '+' || *i == '-' || isdigit(*i)) {
j = i;
state = STATE_COEF;
} else if (*i == 'x' || *i == 'y') {
exp = *i;
j = i + 1;
state = STATE_EXP;
} else {
assert(false);
}
break;
case STATE_COEF:
if (*i == '+' || *i == '-') {
ParseInt(buffer.assign(j, i), 1, &coef);
terms.push_back(Term(coef, xexp, yexp));
--i;
state = STATE_BEGIN;
} else if (*i == 'x' || *i == 'y') {
ParseInt(buffer.assign(j, i), 1, &coef);
exp = *i;
j = i + 1;
state = STATE_EXP;
} else {
assert(isdigit(*i));
}
break;
case STATE_EXP:
// NOTE: Exponents are positive integers.
if (*i == '+' || *i == '-') {
ParseInt(buffer.assign(j, i), 1, exp == 'x' ? &xexp : &yexp);
terms.push_back(Term(coef, xexp, yexp));
--i;
state = STATE_BEGIN;
} else if (*i == 'x' || *i == 'y') {
ParseInt(buffer.assign(j, i), 1, exp == 'x' ? &xexp : &yexp);
exp = *i;
j = i + 1;
state = STATE_EXP;
} else {
assert(isdigit(*i));
}
break;
default:
assert(false);
}
}
return terms;
}
void ParseInt(const std::string& str, int default_value, int* out) {
if (str.empty()) {
*out = default_value;
} else if (str == "+") {
*out = 1;
} else if (str == "-") {
*out = -1;
} else {
*out = std::stoi(str);
}
}
Polynomial operator*(const Polynomial& lhs, const Polynomial& rhs) {
std::map<std::pair<int, int>, int> term_map;
for (const auto& lhs_term : lhs) {
for (const auto& rhs_term : rhs) {
int coef = lhs_term.coef * rhs_term.coef;
int xexp = lhs_term.xexp + rhs_term.xexp;
int yexp = lhs_term.yexp + rhs_term.yexp;
term_map[std::make_pair(xexp, yexp)] += coef;
}
}
Terms terms;
auto convert = [](const std::pair<std::pair<int, int>, int>& term) {
return Term(term.second, term.first.first, term.first.second);
};
std::transform(
term_map.cbegin(), term_map.cend(), std::back_inserter(terms), convert);
return Polynomial(terms);
}
Polynomial::Polynomial(const Terms& terms) {
std::copy_if(terms.cbegin(),
terms.cend(),
std::back_inserter(terms_),
[](const Term& term) { return term.coef != 0; });
std::sort(terms_.begin(), terms_.end());
}
Polynomial::Polynomial(const std::string& expr)
: Polynomial(ParseTerms(expr)) {}
Terms::const_iterator Polynomial::begin() const { return terms_.cbegin(); }
Terms::const_iterator Polynomial::end() const { return terms_.cend(); }
std::ostream& operator<<(std::ostream& out, const Term& term) {
return out << term.coef << 'x' << term.xexp << 'y' << term.yexp;
}
std::ostream& operator<<(std::ostream& out, const Polynomial& polynomial) {
for (const auto& term : polynomial) {
out << ' ' << term;
}
return out;
}