-
Notifications
You must be signed in to change notification settings - Fork 10
/
basic-calculator-iv.cpp
206 lines (189 loc) · 6.32 KB
/
basic-calculator-iv.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
// Time: +: O(d * t), t is the number of terms, d is the average degree of terms
// -: O(d * t)
// *: O(d * t^2)
// eval: O(d * t)
// to_list: O(d * tlogt)
// Space: O(e + d * t), e is the number of evalvars
class Poly {
public:
Poly() {}
Poly(const string& expr) {
vector<string> key;
if (is_number(expr)) {
polies_[key] = stoi(expr);
} else {
key.emplace_back(expr);
++polies_[key];
}
}
Poly operator+(const Poly &rhs) const { // Time: O(d * t)
Poly result;
for (const auto& kvp : polies_) {
result.update(kvp.first, kvp.second);
}
for (const auto& kvp : rhs.polies_) {
result.update(kvp.first, kvp.second);
}
return result;
}
Poly operator-(const Poly &rhs) const { // Time: O(d * t)
Poly result;
for (const auto& kvp : polies_) {
result.update(kvp.first, kvp.second);
}
for (const auto& kvp : rhs.polies_) {
result.update(kvp.first, -kvp.second);
}
return result;
}
Poly operator*(const Poly &rhs) const { // Time: O(d * t^2)
Poly result;
for (const auto& kvp1 : polies_) {
for (const auto& kvp2 : rhs.polies_) {
result.update(merge(kvp1.first, kvp2.first),
kvp1.second * kvp2.second);
}
}
return result;
}
Poly eval(const unordered_map<string, int>& lookup) const { // Time: O(d * t)
Poly result;
for (const auto& kvp : polies_) {
vector<string> key;
int c = kvp.second;
for (const auto& token : kvp.first) {
if (lookup.count(token)) {
c *= lookup.at(token);
} else {
key.emplace_back(token);
}
}
result.update(key, c);
}
return result;
}
operator vector<string>() const { // Time: O(d * tlogt)
map<vector<string>, int, Compare<vector<string>>> sorted(polies_.begin(), polies_.end());
vector<string> result;
for (const auto& kvp : sorted) {
vector<string> tmp(kvp.first);
tmp.emplace(tmp.begin(), to_string(kvp.second));
result.emplace_back(join(tmp, "*"));
}
return result;
}
private:
bool is_number(const std::string &s) const {
return !s.empty() && std::all_of(s.begin(), s.end(), ::isdigit);
}
void update(const vector<string>& key, int val) {
polies_[key] += val;
if (polies_[key] == 0) {
polies_.erase(key);
}
}
vector<string> merge(const vector<string>& arr1, const vector<string>& arr2) const { // Time: O(d)
vector<string> result;
int i = 0, j = 0;
while (i < arr1.size() || j < arr2.size()) {
if (j == arr2.size()) {
result.emplace_back(arr1[i++]);
} else if (i == arr1.size()) {
result.emplace_back(arr2[j++]);
} else if (arr1[i] < arr2[j]) {
result.emplace_back(arr1[i++]);
} else {
result.emplace_back(arr2[j++]);
}
}
return result;
}
string join(const vector<string>& strings, const string& delim) const {
if (strings.empty()) {
return "";
}
ostringstream imploded;
copy(strings.begin(), prev(strings.end()), ostream_iterator<string>(imploded, delim.c_str()));
return imploded.str() + *prev(strings.end());
}
template<typename ContType>
class Compare {
public:
bool operator()
(const ContType& x,const ContType& y) {
return x.size() != y.size() ? x.size() > y.size() : x < y;
}
};
template<typename ContType>
struct Hash {
size_t operator()(const ContType& v) const {
size_t seed = 0;
for (const auto& i : v) {
seed ^= std::hash<typename ContType::value_type>{}(i) + 0x9e3779b9 + (seed<<6) + (seed>>2);
}
return seed;
}
};
unordered_map<vector<string>, int, Hash<vector<string>>> polies_;
};
class Solution {
public:
vector<string> basicCalculatorIV(string expression, vector<string>& evalvars, vector<int>& evalints) {
unordered_map<string, int> lookup;
for (int i = 0; i < evalvars.size(); ++i) {
lookup[evalvars[i]] = evalints[i];
}
return parse(expression).eval(lookup);
}
private:
Poly parse(const string& s) {
if (s.empty()) {
return Poly();
}
stack<Poly> operands;
stack<char> operators;
string operand;
for (int i = s.length() - 1; i >= 0; --i) {
if (isalnum(s[i])) {
operand.push_back(s[i]);
if (i == 0 || !isalnum(s[i - 1])) {
reverse(operand.begin(), operand.end());
operands.emplace(Poly(operand));
operand.clear();
}
} else if (s[i] == ')' || s[i] == '*') {
operators.emplace(s[i]);
} else if (s[i] == '+' || s[i] == '-') {
while (!operators.empty() && operators.top() == '*') {
compute(operands, operators);
}
operators.emplace(s[i]);
} else if (s[i] == '(') {
while (operators.top() != ')') {
compute(operands, operators);
}
operators.pop();
}
}
while (!operators.empty()) {
compute(operands, operators);
}
return operands.top();
}
template<typename T>
void compute(stack<T>& operands, stack<char>& operators) {
const auto left = operands.top();
operands.pop();
const auto right = operands.top();
operands.pop();
const char op = operators.top();
operators.pop();
if (op == '+') {
operands.emplace(left + right);
} else if (op == '-') {
operands.emplace(left - right);
} else if (op == '*') {
operands.emplace(left * right);
}
}
};