#include <bits/stdc++.h>
using namespace std;
bool balanced(string& s) {
int n = s.size();
map < char, int > m;
int j = 0;
int mol = 1;
int aux = int(s[0]) - '0';
if (aux >= 0 && aux <= 9) {
mol = aux;
++j;
}
for (int i = j; i < n && s[i] != '-'; ++i) {
char c = s[i];
if (c == '+') {
++i;
mol = 1;
aux = int(s[i] - '0');
if (aux >= 0 && aux <= 9) {
mol = aux;
} else {
--i;
}
} else if (i < n - 1) {
int sum = 1, val = int(s[i + 1]) - '0';
if (val >= 0 && val <= 9) {
sum = val;
++i;
}
sum *= mol;
if (m.find(c) != m.end())
m[c] += sum;
else
m[c] = sum;
}
j = i;
}
j += 3;
mol = 1;
aux = int(s[j]) - '0';
if (aux >= 0 && aux <= 9) {
mol = aux;
++j;
}
for (int i = j; i < n; ++i) {
char c = s[i];
if (c == '+') {
++i;
mol = 1;
aux = int(s[i] - '0');
if (aux >= 0 && aux <= 9) {
mol = aux;
} else
--i;
} else if (i < n - 1) {
int sum = 1, val = int(s[i + 1]) - '0';
if (val >= 0 && val <= 9) {
sum = val;
++i;
}
sum *= mol;
m[c] -= sum;
if (m[c] == 0) {
m.erase(c);
}
} else {
m[c] -= mol;
if (m[c] == 0) {
m.erase(c);
}
}
}
return m.empty();
}
int main()
{
int n;
cin >> n;
while (n--) {
string s;
cin >> s;
if (balanced(s))
cout << "DA\n";
else
cout << "NE\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
300 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
304 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
300 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
304 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
300 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
304 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
296 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
300 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
304 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |