Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <set>
- #include <map>
- #include <unordered_map>
- #include <unordered_set>
- #include <vector>
- #include <string>
- #include <cmath>
- #include <random>
- #include <algorithm>
- #include <stack>
- #include <functional>
- #include <sstream>
- #include <variant>
- using namespace std;
- vector<string_view> Split(string_view str, string_view delims = " ")
- {
- vector<string_view> output;
- //output.reserve(str.size() / 2);
- for (auto first = str.data(), second = str.data(), last = first + str.size(); second != last && first != last; first = second + 1) {
- second = find_first_of(first, last, cbegin(delims), cend(delims));
- if (first != second)
- output.emplace_back(first, second - first);
- }
- return output;
- }
- int Sum(int l, int r) {
- return l + r;
- }
- int Minus(int l, int r) {
- return l - r;
- }
- int Mult(int l, int r) {
- return l * r;
- }
- int Div(int l, int r) {
- if (r == 0) {
- return l;
- }
- return l / r;
- }
- int Less(int l, int r) {
- return l < r;
- }
- int Equal(int l, int r) {
- return l == r;
- }
- int Greater(int l, int r) {
- return l > r;
- }
- int IfOper(int check, int l, int r) {
- if (check) {
- return l;
- }
- return r;
- }
- int main() {
- cin.tie(nullptr);
- ios_base::sync_with_stdio(false);
- unordered_map<char, function<int(int, int)>> bind = {
- {'+', Sum}
- , {'-', Minus}
- , {'*', Mult}
- , {'/', Div}
- , {'<', Less}
- , {'=', Equal}
- , {'>', Greater}
- };
- size_t N = 0;
- cin >> N;
- string dummy;
- string input;
- getline(cin, dummy);
- getline(cin, input);
- vector<string_view> strs = Split(input);
- vector<variant<char, int>> vars;
- vars.reserve(strs.size());
- for (auto& str: strs) {
- if (('0' <= str[0] && str[0] <= '9') || (str.size() > 1 && '0' <= str[1] && str[1] <= '9')) {
- vars.emplace_back((int)stoi(string(str)));
- } else {
- vars.emplace_back((char)str[0]);
- }
- }
- vector<int> params(N);
- size_t T = 0;
- cin >> T;
- for (size_t i = 0; i < T; ++i) {
- for (size_t j = 0; j < N; ++j) {
- cin >> params[j];
- }
- stack<int> nums;
- for (auto& var: vars) {
- if (holds_alternative<int>(var)) {
- nums.push(get<int>(var));
- } else {
- char symb = get<char>(var);
- if (isalpha(symb)) {
- nums.push(params[symb - 'a']);
- } else {
- int r = nums.top();
- nums.pop();
- int l = nums.top();
- nums.pop();
- if (symb == '?') {
- int check = nums.top();
- nums.pop();
- nums.push(IfOper(check, l, r));
- } else {
- nums.push(bind[symb](l, r));
- }
- }
- }
- }
- if (nums.empty()) {
- cout << 0 << '\n';
- } else {
- cout << nums.top() << '\n';
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement