Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- deque<int> first, last;
- bool left_is_open = true, right_is_open = true;
- void balance() {
- while (first.size() > last.size() + 1) {
- last.push_back(first.back());
- first.pop_back();
- }
- while (last.size() > first.size()) {
- first.push_back(last.back());
- last.pop_back();
- }
- }
- void check(bool ff) {
- if (ff) {
- if (first.size() >= last.size() + 1) {
- last.push_back(first.back());
- first.pop_back();
- }
- } else {
- if (last.size() >= first.size() + 1) {
- first.push_back(last.back());
- last.pop_back();
- }
- }
- }
- void solve() {
- int n;
- cin >> n;
- string str;
- cin >> str;
- int cnt = 1;
- // int ans1 = 0, ans2 = 0;
- deque<int> a, b;
- forn(i, 0, n) {
- if (str[i] == 'a') {
- if (left_is_open && right_is_open) {
- first.push_back(cnt++);
- } else {
- last.push_front(cnt++);
- // balance();
- }
- } else if (str[i] == 'b') {
- if (left_is_open && right_is_open) {
- last.push_back(cnt++);
- } else {
- first.push_front(cnt++);
- // balance();
- }
- } else if (str[i] == 'A') {
- int res = 0;
- if (!first.empty()) {
- res = first.front();
- first.pop_front();
- } else {
- res = last.back();
- last.pop_back();
- }
- // ans1 = res;
- // dbg(res);
- cout << res % 10;
- } else if (str[i] == 'B') {
- int res = 0;
- if (!last.empty()) {
- res = last.front();
- last.pop_front();
- } else {
- res = first.back();
- first.pop_back();
- }
- // balance();
- // ans1 = res;
- // dbg(res);
- cout << res % 10;
- } else if (str[i] == '>') { // 1
- left_is_open = false;
- balance();
- } else if (str[i] == ']') { // 2
- right_is_open = false;
- balance();
- } else if (str[i] == '<') { // 1
- left_is_open = true;
- balance();
- check(true);
- } else if (str[i] == '[') { // 2
- right_is_open = true;
- balance();
- check(false);
- }
- // cout << "frst\n";
- // for (int cf: first) {
- // cout << cf << ' ';
- // }
- // cout << "\nlast\n";
- // for (int cf: last) {
- // cout << cf << ' ';
- // }
- // cout << '\n';
- }
- cout << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement