QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#215738 | #6750. Calculate | Eunoiay# | WA | 0ms | 3824kb | C++17 | 1.5kb | 2023-10-15 13:17:28 | 2023-10-15 13:17:29 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
void solve() {
string s;
cin >> s;
int n = s.size();
array<char, 10> v = {'0', '9'};
array<int , 2> k = {};
vector<string> str;
if (count(s.begin(), s.end(), '?')) {
k[0] = find(s.begin(), s.end(), '?') - s.begin();
k[1] = n - 1 - (find(s.rbegin(), s.rend(), '?') - s.rbegin());
for (int i = 0; i < 2; ++ i) {
for (int j = 0; j < 2; ++ j) {
s[k[0]] = v[i];
s[k[1]] = v[j];
str.push_back(s);
// cerr << "s: " << s << "\n";
s[k[0]] = '?';
s[k[1]] = '?';
}
}
} else {
str.push_back(s);
}
int ok = 0;
for(int i = 1; i < (int)s.size(); i ++) ok |= (isdigit(s[i]) && isdigit(s[i - 1]));
assert(!ok);
int ans = -1 << 30;
for (const auto &t : str) {
stack<char> op;
stack<int> stk;
auto eval = [&]() {
while (!op.empty()) {
char o = op.top();
op.pop();
if (o == '(') {
break;
}
int x = stk.top(); stk.pop();
int y = stk.top(); stk.pop();
if (o == '+') {
stk.push(x + y);
} else {
stk.push(y - x);
}
}
};
for (char c : t) {
if (c == ')') {
eval();
} else if (isdigit(c)) {
stk.push(c - '0');
} else {
op.push(c);
}
}
eval();
if (!stk.empty()) {
ans = max(ans, stk.top());
}
}
cout << ans << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3824kb
input:
?+?
output:
18
result:
ok 1 number(s): "18"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3632kb
input:
(?+9)-(?+1)
output:
17
result:
ok 1 number(s): "17"
Test #3:
score: -100
Wrong Answer
time: 0ms
memory: 3596kb
input:
((9)-(((8)-(2))+(((1+(1))-(1+((2)+2+2)))+(5)+4))+(((7)-((9)+3))-((8)-(0-(2))+0))+((6)-(6+(((4)-(9))-(8-((9)+(1))+(0)))+(2-((9)+7))-(1)))-((((7)+(1))-((3)+(3)))-((2)-((6)-((3)-(8)))))+(2+0-((6)-(1))))-((((3)-(((0)+((4)-(9))+((6+8)+4)+(5)-(4-(3)-(8)))-((8)-(2))))+(((2)-(4))+(6)-(2))+(6-(1))-((2+9)-(3+...
output:
-151
result:
wrong answer 1st numbers differ - expected: '-63', found: '-151'