Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdio>
- #include <string>
- #include <vector>
- #include <stack>
- #include <sstream>
- using namespace std;
- string Subst(string& initial, char begin, char end)
- {
- long idxBegin = initial.find(begin) + 1;
- long idxEnd = initial.rfind(end);
- return initial.substr(idxBegin, idxEnd - idxBegin);
- }
- void evalPopOrTop(string& _current, vector<stack<long>>& _as, bool doPop)
- {
- istringstream strUsedStack(Subst(_current, '(', ')'));
- long idxUsedStack;
- strUsedStack >> idxUsedStack;
- idxUsedStack--;
- cout << _as[idxUsedStack].top() << " ";
- if (doPop) _as[idxUsedStack].pop();
- }
- void evalPush(string& _current, vector<stack<long>>& _as)
- {
- istringstream strUsedStack(Subst(_current, '(', ','));
- istringstream strValue(Subst(_current, ',', ')'));
- long idxUsedStack, value;
- strUsedStack >> idxUsedStack;
- strValue >> value;
- idxUsedStack--;
- _as[idxUsedStack].push(value);
- }
- long main(void)
- {
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- long stackCount, opCount;
- cin >> stackCount >> opCount;
- vector<stack<long> > allStacks(stackCount);
- string _current;
- getline(cin, _current);
- for (long i = 0; i < opCount; i++)
- {
- getline(cin, _current);
- if (_current[0] == 'T') evalPopOrTop(_current, allStacks, false);
- else if (_current[1] == 'O') evalPopOrTop(_current, allStacks, true);
- else evalPush(_current, allStacks);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment