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 "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 n, m 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 ().

先预处理出每个(对应的),每个)对应的(

这个用个栈就好

维护一个像是双端链表的东西,左右移就直接在链表上移,对于每个删除操作,把它到它对应的符号的位置一段全删掉即可

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstring>
 4 #include<cstdlib>
 5 #include<algorithm>
 6 #include<cmath>
 7 #include<queue>
 8 #include<deque>
 9 #include<set>
10 #include<map>
11 #include<ctime>
12 #define LL long long
13 #define inf 0x7ffffff
14 #define pa pair<int,int>
15 #define mkp(a,b) make_pair(a,b)
16 #define pi 3.1415926535897932384626433832795028841971
17 using namespace std;
18 inline LL read()
19 {
20     LL x=0,f=1;char ch=getchar();
21     while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
22     while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
23     return x*f;
24 }
25 int n,m,pos;
26 int go[500010];
27 char s[500010];
28 char op[500010];
29 int zhan[500010],top;
30 int d[500010],l[500010],r[500010];
31 int main()
32 {
33     n=read();m=read();pos=read();
34     scanf("%s",s+1);
35     scanf("%s",op+1);
36     for (int i=1;i<=n;i++)
37     {
38         if (s[i]==‘(‘)zhan[++top]=i;
39         else
40         {
41             go[i]=zhan[top];
42             go[zhan[top]]=i;
43             top--;
44         }
45         d[i]=s[i]==‘(‘;
46         if (i!=1)l[i]=i-1;
47         if (i!=n)r[i]=i+1;
48     }
49     for (int i=1;i<=m;i++)
50     {
51         if (op[i]==‘L‘){if (l[pos])pos=l[pos];}
52         if (op[i]==‘R‘){if (r[pos])pos=r[pos];}
53         if (op[i]==‘D‘)
54         {
55             int nex=go[pos];
56             if (nex<pos)swap(nex,pos);
57             l[r[nex]]=l[pos];
58             r[l[pos]]=r[nex];
59             if (r[nex])pos=r[nex];else pos=l[pos];
60         }
61     }
62     while (l[pos])pos=l[pos];
63     if (!pos){puts("");return 0;}
64     while (r[pos])
65     {
66         printf("%c",d[pos]==1?‘(‘:‘)‘);
67         pos=r[pos];
68     }
69     printf("%c",d[pos]==1?‘(‘:‘)‘);
70 }

cf 670E

时间: 2024-08-24 18:28:31

cf670E 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) 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 670E Correct Bracket Sequence Editor (list模拟)

题意 给出一个合法的小括号序列,然后有三种操作: L 光标左移 R 光标右移 D 删除当前位置括号并他的对应括号,两括号中间的也一起删除. 输出操作后的序列. 思路 list直接模拟就好了,这题主要考察的也就是list的操作吧. list的删除也确实是个坑,注意光标移动出界之后别忘了向左移动一下. 代码 #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm>

CodeForces 670E Correct Bracket Sequence Editor

链表,模拟. 写一个双向链表模拟一下过程. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<vector> #include<map> #include<set> #include&

Gym 240084E - Correct Bracket Sequence Editor - [线段树]

题目链接:https://codeforces.com/gym/240084/problem/E 题意: 给出一个已经匹配的括号串,给出起始的光标位置(光标总是指向某个括号). 有如下操作: 1.往左移动一下光标: 2.往左移动一下光标: 3.删除当前光标指向的括号,以及和它匹配的那个括号,以及这两个括号之间的所有括号. 要求你给出在做完所有操作后的括号串. 题解: 用线段树维护,每个括号是否存在,存在记为 $1$,被删掉了记为 $0$. 然后我们只需要实现:①区间求和.②区间赋值.③根据 $k

Codeforces Beta Round #5 C. Longest Regular Bracket Sequence 栈/dp

C. Longest Regular Bracket Sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/problem/5/C Description This is yet another problem dealing with regular bracket sequences. We should remind you that a bracket sequence

cf3D Least Cost Bracket Sequence

This is yet another problem on regular bracket sequences. A bracket sequence is called regular, if by inserting "+" and "1" into it we get a correct mathematical expression. For example, sequences "(())()", "()" and

贪心+stack Codeforces Beta Round #5 C. Longest Regular Bracket Sequence

题目传送门 1 /* 2 题意:求最长括号匹配的长度和它的个数 3 贪心+stack:用栈存放最近的左括号的位置,若是有右括号匹配,则记录它们的长度,更新最大值,可以在O (n)解决 4 详细解释:http://blog.csdn.net/taoxin52/article/details/26012167 5 */ 6 #include <cstdio> 7 #include <algorithm> 8 #include <cstring> 9 #include <

D - Replace To Make Regular Bracket Sequence

You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bra