codeforces 670E Correct Bracket Sequence Editor

Recently Polycarp started to develop a text editor that works only with correct bracket sequences (abbreviated as CBS).

Note that a bracket sequence is correct if it is possible to get a correct mathematical expression by adding "+"-s and "1"-s to it. For example, sequences "(())()", "()" and "(()(()))" are correct, while ")(", "(()" and "(()))(" are not. Each bracket in CBS has a pair. For example, in "(()(()))":

  • 1st bracket is paired with 8th,
  • 2d bracket is paired with 3d,
  • 3d bracket is paired with 2d,
  • 4th bracket is paired with 7th,
  • 5th bracket is paired with 6th,
  • 6th bracket is paired with 5th,
  • 7th bracket is paired with 4th,
  • 8th bracket is paired with 1st.

Polycarp's editor currently supports only three operations during the use of CBS. The cursor in the editor takes the whole position of one of the brackets (not the position between the brackets!). There are three operations being supported:

  • «L» — move the cursor one position to the left,
  • «R» — move the cursor one position to the right,
  • «D» — delete the bracket in which the cursor is located, delete the bracket it's paired to and all brackets between them (that is, delete a substring between the bracket in which the cursor is located and the one it's paired to).

After the operation "D" the cursor moves to the nearest bracket to the right (of course, among the non-deleted). If there is no such bracket (that is, the suffix of the CBS was deleted), then the cursor moves to the nearest bracket to the left (of course, among the non-deleted).

There are pictures illustrated several usages of operation "D" below.

All incorrect operations (shift cursor over the end of CBS, delete the whole CBS, etc.) are not supported by Polycarp's editor.

Polycarp is very proud of his development, can you implement the functionality of his editor?

Input

The first line contains three positive integers nm and p (2 ≤ n ≤ 500 0001 ≤ m ≤ 500 0001 ≤ p ≤ n) — the number of brackets in the correct bracket sequence, the number of operations and the initial position of cursor. Positions in the sequence are numbered from left to right, starting from one. It is guaranteed that n is even.

It is followed by the string of n characters "(" and ")" forming the correct bracket sequence.

Then follow a string of m characters "L", "R" and "D" — a sequence of the operations. Operations are carried out one by one from the first to the last. It is guaranteed that the given operations never move the cursor outside the bracket sequence, as well as the fact that after all operations a bracket sequence will be non-empty.

Output

Print the correct bracket sequence, obtained as a result of applying all operations to the initial sequence.

Examples
input
8 4 5
(())()()
RDLD
output
()
input
12 5 3
((()())(()))
RRDLD
output
(()(()))
input
8 8 8
(())()()
LLLLLLDD
output
()()
Note

In the first sample the cursor is initially at position 5. Consider actions of the editor:

  1. command "R" — the cursor moves to the position 6 on the right;
  2. command "D" — the deletion of brackets from the position 5 to the position 6. After that CBS takes the form (())(), the cursor is at the position 5;
  3. command "L" — the cursor moves to the position 4 on the left;
  4. command "D" — the deletion of brackets from the position 1 to the position 4. After that CBS takes the form (), the cursor is at the position 1.

Thus, the answer is equal to ().

题意: 给定n个括号,给定m次操作,给定初始位置,求操作后剩下的括号

思路:首先记录每一个括号的左右,并且记录每一个括号和他匹配括号的 位置,进行模拟,R表示当前位置向右移动一个位置,L表示向左移动一个位置,D表示删除当前括号和他匹配的括号,这种模拟好绕,很难理解。。。。。。加油!!!只要坚持,一定会收获的更多!!!

AC代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <map>
#include <cstring>
#include <stack>

using namespace std;

int l[500005],r[500005];//记录每一个括号的左右
char c[500005];//输入的括号
int a[500005];
int n,m,p;

int main() {
    cin >> n >> m >> p;//p是当前的位置
    cin >> (c+1);
    memset(a,0,sizeof(a));
    stack <int>s;
    for(int i = 0; i <= n+2; i++) {
        l[i] = i-1;
        r[i] = i+1;
    }
    for(int i = 1; i <= n; i++) {//通过栈来获取当前的括号匹配的位置
        if(c[i] == '(') {
            s.push(i);
        } else {
            int sp = s.top();
            a[i] = sp;
            a[sp] = i;
            s.pop();
        }
    }
    char op;
    while(m--) {
        cin >> op;
        if(op == 'R') {
            p = r[p];
        }
        if(op == 'L') {
            p = l[p];
        }
        if(op == 'D') {
            int L,R;
            if(c[p] == '(') {//左括号,那么删除与它匹配的括号,光标的位置变为它的匹配的右括号,
                L = l[p];
                R = r[a[p]];
                p = R;
            } else {
                L = l[a[p]];
                R = r[p];
                p = r[p];
            }
            r[L] = R;
            l[R] = L;
            if(p > n) {
                p = l[p];
            }
        }
    }
    for(int i = r[0]; i <= n; i=r[i]) {
        cout << c[i];
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值