QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#191224 | #7525. Brackets | Days_of_Future_Past# | WA | 1ms | 5856kb | C++14 | 1.3kb | 2023-09-29 20:43:39 | 2023-09-29 20:43:39 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int N = 4e6+10;
char ip[N];
int n;
int n_plus_back[N];
char ans[N];
int main() {
scanf("%s", ip+1);
n = strlen(ip + 1);
for (int i = n; i > 0; --i) {
if (ip[i] == '+')
n_plus_back[i] = 1 + n_plus_back[i + 1];
}
int it = 0;
for (int i = 1; i <= n;) {
int var_pos;
int nxt_var;
int cnt_pre_incr;
int cnt_post_incr;
if (n_plus_back[i] == 2) {
cnt_pre_incr = 1;
var_pos = i + 2;
} else { // n_plus_back[i] == 0
cnt_pre_incr = 0;
var_pos = i;
}
if (n_plus_back[var_pos + 1] >= 2) {
cnt_post_incr = 1;
nxt_var = var_pos + 4;
} else {
cnt_post_incr = 0;
nxt_var = var_pos + 2;
}
for (int k = 0; k < cnt_post_incr; ++k) {
ans[++it] = '(';
}
for (int k = 0; k < cnt_pre_incr; ++k) {
ans[++it] = '(';
ans[++it] = '+';
ans[++it] = '+';
}
ans[++it] = ip[var_pos];
for (int k = 0; k < cnt_pre_incr; ++k) {
ans[++it] = ')';
}
for (int k = 0; k < cnt_post_incr; ++k) {
ans[++it] = '+';
ans[++it] = '+';
ans[++it] = ')';
}
if (nxt_var <= n) {
ans[++it] = '+';
}
i = nxt_var;
}
printf("%s\n", ans + 1);
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 5796kb
input:
x+++y
output:
(x++)+y
result:
ok Participant's answer is correct
Test #2:
score: 0
Accepted
time: 1ms
memory: 5848kb
input:
q+u+++h+++++o+q++
output:
q+(u++)+(h++)+(++o)+(q++)
result:
ok Participant's answer is correct
Test #3:
score: -100
Wrong Answer
time: 1ms
memory: 5856kb
input:
++a+++b+++c+++d+e+++f+++g+h+j+k+++++i
output:
((++a)++)+(b++)+(c++)+d+(e++)+(f++)+g+h+j+(k++)+(++i)
result:
wrong answer Division by operands in answer is wrong, position: 5