This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |