Submission #674957

#TimeUsernameProblemLanguageResultExecution timeMemory
674957AriadnaKemija (COCI22_kemija)C++14
50 / 50
1 ms304 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...