QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#421531 | #6750. Calculate | Fugeg# | WA | 0ms | 3788kb | C++17 | 4.0kb | 2024-05-25 20:45:17 | 2024-05-25 20:45:17 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e3 + 10, M = 4e5;
string s;
stack<int> st;
stack<char> stc;
ll ans = -999999999;
void solve()
{
cin >> s;
int cnt = 0;
for (int i = 0; i < s.size(); i++)
{
if (s[i] == '?')
cnt++;
}
if (cnt == 0)
{
for (int i = 0; i < s.size(); i++)
{
if (s[i] >= '0' && s[i] <= '9')
{
st.push(s[i] - '0');
}
else
{
if (s[i] == '(' || s[i] == '+' || s[i] == '-')
{
stc.push(s[i]);
}
else
{
while (1)
{
int a1 = st.top();
st.pop();
char c = stc.top();
stc.pop();
int re;
if (c == '(')
{
st.push(a1);
break;
}
int a2 = st.top();
st.pop();
if (c == '+')
{
re = a1 + a2;
}
else if (c == '-')
{
re = a2 - a1;
}
st.push(re);
}
}
}
}
while (st.size() > 1)
{
int a1 = st.top();
st.pop();
int a2 = st.top();
st.pop();
char c = stc.top();
stc.pop();
int re = 0;
if (c == '+')
{
re = a1 + a2;
}
else
{
re = a2 - a1;
}
st.push(re);
}
cout << st.top();
}
else if (cnt == 1)
{
for (int w1 = 0; w1 <= 9; w1++)
{
while (st.size())
st.pop();
while (stc.size())
stc.pop();
for (int i = 0; i < s.size(); i++)
{
if (s[i] >= '0' && s[i] <= '9')
{
st.push(s[i] - '0');
}
else if (s[i] == '?')
{
st.push(w1);
}
else
{
if (s[i] == '(' || s[i] == '+' || s[i] == '-')
{
stc.push(s[i]);
}
else
{
while (1)
{
int a1 = st.top();
st.pop();
char c = stc.top();
stc.pop();
int re;
if (c == '(')
{
st.push(a1);
break;
}
int a2 = st.top();
st.pop();
if (c == '+')
{
re = a1 + a2;
}
else if (c == '-')
{
re = a2 - a1;
}
st.push(re);
}
}
}
}
while (st.size() > 1)
{
int a1 = st.top();
st.pop();
int a2 = st.top();
st.pop();
char c = stc.top();
stc.pop();
int re = 0;
if (c == '+')
{
re = a1 + a2;
}
else
{
re = a2 - a1;
}
st.push(re);
}
ans = max(ans, (ll)st.top());
}
cout << ans;
}
else if (cnt == 2)
{
int f = 0;
for (int w1 = 0; w1 <= 9; w1++)
{
for (int w2 = 0; w2 <= 9; w2++)
{
f = 0;
while (st.size())
st.pop();
while (stc.size())
stc.pop();
for (int i = 0; i < s.size(); i++)
{
if (s[i] >= '0' && s[i] <= '9')
{
st.push(s[i] - '0');
}
else if (s[i] == '?')
{
if (f == 0)
{
st.push(w1);
f = 1;
}
else
st.push(w2);
}
else
{
if (s[i] == '(' || s[i] == '+' || s[i] == '-')
{
stc.push(s[i]);
}
else
{
while (1)
{
int a1 = st.top();
st.pop();
char c = stc.top();
stc.pop();
int re;
if (c == '(')
{
st.push(a1);
break;
}
int a2 = st.top();
st.pop();
if (c == '+')
{
re = a1 + a2;
}
else if (c == '-')
{
re = a2 - a1;
}
st.push(re);
}
}
}
}
while (st.size() > 1)
{
int a1 = st.top();
st.pop();
int a2 = st.top();
st.pop();
char c = stc.top();
stc.pop();
int re = 0;
if (c == '+')
{
re = a1 + a2;
}
else
{
re = a2 - a1;
}
st.push(re);
}
ans = max(ans, (ll)st.top());
}
}
cout << ans;
}
}
int main()
{
std::ios::sync_with_stdio(0);
std::cin.tie(0);
std::cout.tie(0);
int t = 1;
// cin >> t;
while (t--)
solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3780kb
input:
?+?
output:
18
result:
ok 1 number(s): "18"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3524kb
input:
(?+9)-(?+1)
output:
17
result:
ok 1 number(s): "17"
Test #3:
score: -100
Wrong Answer
time: 0ms
memory: 3788kb
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'