This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//author: erray
#include <bits/stdc++.h>
using namespace std;
template<typename A, typename B> string to_string(pair<A, B>);
template<typename A, typename B, typename C> string to_string(tuple<A, B, C>);
string to_string(string s) {
return '"' + s + '"';
}
string to_string(const char* c) {
return to_string(string(c));
}
string to_string(char c) {
return string("'") + c + "'";
}
string to_string(bool b) {
return (b ? "true" : "false");
}
template<size_t T>
string to_string(bitset<T> bs) {
return bs.to_string();
}
template<typename T>
string to_string(priority_queue<T> pq) {
vector<T> a;
while (!pq.empty()) {
a.push_back(pq.top());
pq.pop();
}
return to_string(a);
}
string to_string(vector<bool> v) {
string res = "{";
for (int i = 0; i < int(v.size()); ++i) {
if (int(res.size()) > 1) {
res += ", ";
}
res += to_string(v[i]);
}
res += "}";
return res;
}
template<typename T>
string to_string(T v) {
string res = "{";
for (auto x : v) {
if (int(res.size()) > 1) {
res += ", ";
}
res += to_string(x);
}
res += "}";
return res;
}
template<typename A, typename B> string to_string(pair<A, B> p) {
return '(' + to_string(p.first) + ", " + to_string(p.second) + ')';
}
template<typename A, typename B, typename C> string to_string(tuple<A, B, C> t) {
return '(' + to_string(get<0>(t)) + ", " + to_string(get<1>(t)) + ", " + to_string(get<2>(t)) + ')';
}
void debug_out() {
cerr << endl;
}
template<typename H, typename... T>
void debug_out(H head, T... tail) {
cerr << " " << to_string(head);
debug_out(tail...);
}
#ifdef DEBUG
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) void(37)
#endif
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
string S;
cin >> S;
int s = int(S.size());
int n = count(S.begin(), S.end(), 'm') + count(S.begin(), S.end(), '?');
vector<vector<int>> g(n);
vector<int> last(2 * s + 2, -1);
vector<int> id(s, -1);
vector<int> type(n);
int sum = 0;
int t = n - 1;
for (int i = s - 1; i >= 0; --i) {
debug(i, S[i], sum);
if (S[i] == 'm') {
id[i] = t--;
type[id[i]] = (S[i + 1] == 'a');
g[id[i]].push_back(id[i + 4]);
assert(last[sum + s + 1] != -1);
g[id[i]].push_back(last[sum + s + 1]);
} else if (S[i] == ',') {
debug(sum, id[i + 1]);
last[sum + s] = id[i + 1];
} else if (S[i] == '?') {
id[i] = t--;
} else if (S[i] == '(') {
sum -= 1;
} else if (S[i] == ')') {
sum += 1;
}
}
vector<int> size(n);
vector<int> l(n, 0);
vector<int> r(n, 0);
for (int i = n - 1; i >= 0; --i) {
if (g[i].empty()) {
size[i] = 1;
continue;
}
int x = g[i][0], y = g[i][1];
size[i] += size[x] + size[y];
debug(i, size[i], x, y);
if (size[i] == 2) {
if (type[i]) {
l[i] = 1;
} else {
r[i] = 1;
}
} else if (type[i]) {
r[i] = min(r[x], r[y]);
l[i] = l[x] + l[y] + 1;//?
} else {
l[i] = min(l[x], l[y]);
r[i] = r[x] + r[y] + 1;
}
}
cout << size[0] - l[0] - r[0] << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |