Advertisement
leoanjos

Correct Bracket Sequence Editor

Feb 28th, 2021
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.36 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define long long long int
  6.  
  7. #define pii pair<int, int>
  8. #define pll pair<long, long>
  9. #define fst first
  10. #define snd second
  11.  
  12. #define all(ds) (ds).begin(), (ds).end()
  13. #define size(ds) (int) (ds).size()
  14.  
  15. const int MAX = 5e5 + 5;
  16.  
  17. int main() {
  18.     ios_base::sync_with_stdio(false);
  19.     cin.tie(NULL);
  20.  
  21.     int n, m, p;
  22.     cin >> n >> m >> p;
  23.  
  24.     string s; cin >> s;
  25.  
  26.     set<int> indices;
  27.     stack<pair<char, int>> aux;
  28.  
  29.     int matches[MAX];
  30.     for (int i = 0; i < size(s); i++) {
  31.         indices.insert(i);
  32.         if (s[i] == '(') aux.push({ s[i], i });
  33.         else {
  34.             matches[aux.top().second] = i;
  35.             matches[i] = aux.top().second;
  36.             aux.pop();
  37.         }
  38.     }
  39.  
  40.     auto it = indices.begin();
  41.     while (--p) it++;
  42.  
  43.     string ops; cin >> ops;
  44.     for (auto c: ops) {
  45.         if (c == 'L') it--;
  46.         else if (c == 'R') it++;
  47.         else {
  48.             int first = *it, last = matches[*it];
  49.             if (first > last) swap(first, last);
  50.  
  51.             indices.erase(indices.lower_bound(first), indices.upper_bound(last));
  52.  
  53.             it = indices.upper_bound(last);
  54.             if (it == indices.end())
  55.                 it = indices.lower_bound(first), it--;
  56.         }
  57.     }
  58.  
  59.     for (auto idx: indices)
  60.         cout << s[idx];
  61.  
  62.     cout << "\n";
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement