Codeforces Round #350 (Div. 2) E. Correct Bracket Sequence Editor

E. 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 000, 1 ≤ m ≤ 500 000, 1 ≤ 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 ().

#include<cstdio>
#include<cstring>
#include<cstring>
#include<stack>
#include<algorithm>
#define fi first
#define se second
using namespace std;
const int maxn=5e5+5;
char s[maxn],op[maxn];
struct Node
{
    char c;
    int pre,next,pi,id;
}p[maxn];
stack<pair<char,int> >S;
int main()
{
    int n,m,k;
    scanf("%d%d%d",&n,&m,&k);
    scanf("%s",s+1);
    p[0].next=1,p[n+1].pre=n;
    p[0].c=p[n+1].c=‘#‘;
    p[0].id=0,p[n+1].id=n+1;
    for(int i=1;i<=n;i++)
        p[i].next=i+1,p[i].pre=i-1,p[i].c=s[i],p[i].id=i;
    for(int i=1;i<=n;i++)
    {
        if(S.empty()||s[i]==S.top().fi)
            S.push(make_pair(s[i],i));
        else
        {
            pair<char,int> e=S.top();
            S.pop();
            p[i].pi=e.se;
            p[e.se].pi=i;
        }
    }
    int cur=k;
    scanf("%s",op);
    for(int i=0;i<m;i++)
    {
        if(op[i]==‘L‘)cur=p[cur].pre;
        else if(op[i]==‘R‘)cur=p[cur].next;
        else
        {
            if(p[cur].c==‘(‘)
            {
                Node &e1=p[p[cur].pre],&e2=p[p[p[cur].pi].next];
                e1.next=e2.id;
                e2.pre=e1.id;
                if(e2.id==n+1)cur=e1.id;
                else cur=e2.id;
            }
            else
            {
                Node &e1=p[p[cur].next],&e2=p[p[p[cur].pi].pre];
                e1.pre=e2.id;
                e2.next=e1.id;
                if(e1.id==n+1)cur=e2.id;
                else cur=e1.id;
            }
        }
    }
    for(int i=p[0].next;i!=n+1;i=p[i].next)
        printf("%c",p[i].c);
    return 0;
}
时间: 2024-12-07 11:00:20

Codeforces Round #350 (Div. 2) E. Correct Bracket Sequence Editor的相关文章

Codeforces Round #350 (Div. 2) E. Correct Bracket Sequence Editor 线段树模拟

E. 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 express

【解题报告】Codeforces Round #350 (Div. 2)

题目链接 A.Holidays(Codeforces 670A) 思路 首先,若 7 能整除 n 的话,最小假期天数 m1 与最大假期天数 m2 都等于 2×n7 ."整除"提示我们可以根据 n 除以 7 的余数来分类: 余数为 0 , m1=m2=2×n7 . 余数为 1 ,考虑多出来的那天是不是周末, m1=2×n7,m2=2×n7+1 . 余数为 6 ,考虑多出来的6天中必然有周末,考虑有1天周末还是2天周末, m1=2×n7+1,m2=2×n7+2 . 其它余数,考虑多出来的那

Codeforces Round #350 (Div. 2) 题解

Holidays Game of Robots Cinema Magic Powder - 12 Correct Bracket Sequence Editor Restore a Number Holidays #include<bits/stdc++.h> using namespace std; #define For(i,n) for(int i=1;i<=n;i++) #define Fork(i,k,n) for(int i=k;i<=n;i++) #define Re

cf670E 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 "

Codeforces Round #350 (Div. 2) F. Restore a Number 模拟构造题

F. Restore a Number Vasya decided to pass a very large integer n to Kate. First, he wrote that number as a string, then he appended to the right integer k — the number of digits in n. Magically, all the numbers were shuffled in arbitrary order while

Codeforces Round #350 (Div. 2) E 思维模拟

给出一个合法的括号串 有LRD三种操作 LR分别是左右移动当前位置 且合法 D为删除这个括号和里面的所有 当删除完成后 位置向右移动 如果不能移动 就向左 比赛都是很久远的事情了 写这道题也是一时兴起 先写了一发暴力 先拿a记录下来与这个括号相匹配的括号位置 拿vis记录括号是否被删除 每次碰到false的括号就整体跳过 然后一气跑到了test78才停下来 想了想暴力肯定不行 就加了l数组和r数组 来记录当前位置的左右仍存在的括号在哪 每次进行移动和删除的时候都利用这个进行移动 顺便更改 有一种

Codeforces Round #350 (Div. 2) D2 二分

五一期间和然然打的团队赛..那时候用然然的号打一场掉一场...七出四..D1是个数据规模较小的题 写了一个暴力过了 面对数据如此大的D2无可奈何 现在回来看 一下子就知道解法了 二分就可以 二分能做多少个 每次对mid求一下不够的差值 比较差值与m的大小进行l与r的变换 由于自己一向对二分比较迷茫 自己琢磨出来一套神奇的办法面对边界数据 当小于和大于的时候 抛弃mid值 当等于的时候 直接break 然后打一发while试试能否向更好的情况偏移 当然在这个题目中 如果是直接break的时候就不用

Codeforces Round #350 (Div. 2)

A. Holidays 题意:一个礼拜有两天放假,那么n天最多和最少分别有多少天假呢? 题解:首先算最少的,就是n%7,如果余6的话则要+1.最多则是尽量让余下的天数为假期,如果余一天就加一天,余二就加两天,大于二依然只加两天,直到余六再加一天. 代码: 1 /*A*/ 2 #include<cstdio> 3 using namespace std; 4 5 int main() 6 { 7 int n; 8 while(scanf("%d",&n)!=EOF)

Codeforces Round #350 (Div. 2) E (跳转链表)

[题意]一个括号序列,你有以下三种操作:"L"光标左移:"R"光标右移:"D"删掉这个括号以及和它对应的括号之间的所有括号,并且把光标移动到被删后右边的那个括号上.如果没有了,就移动到串的末尾.现在给你原括号序列和操作,求最终结果! [分析]由于操作最多有50万个,那么简单想一下用链表来暴力模拟都是能过得去的.那么,我在这里用了类似于跳转链表来模拟这个过程!但是这里我并不是一个一个的跳转的,而是预处理了最大的括号匹配,直接跳转到可以到达的最大位置