E. Correct Bracket Sequence Editor

time limit per test           2 seconds

memory limit per test     256 megabytes

题目链接:https://codeforces.com/gym/240084/problem/E

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 ().

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <stack>
#include <map>
#include <set>
#include <cassert>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
typedef vector<int> VI;
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<long, long> PLL;
const ll mod = 1000000007;
const int N = 5e5 + 5; // 矩阵最大维数
string S;
stack<int> q;struct node {char a;int mac;
}s[N];
int pre[N];
int  NEXT[N];
char kh[N];
int n, m, p;
int main() {cin >> n >> m >> p;cin >> S;scanf("%s", kh);rep(i, 0, n) {s[i + 1].a = S[i];if (S[i] == '(') q.push(i + 1);else {s[i + 1].mac = q.top();q.pop();}}for (int i = n - 1; i >= 0; i--) {if (S[i] == ')') q.push(i + 1);else {s[i + 1].mac = q.top();q.pop();}}int cursor = p;rep(i, 0, n + 1) {NEXT[i] = i + 1;pre[i] = i - 1;}for (int i = 0; i<m; i++) {if (kh[i] == 'R') cursor = NEXT[cursor];else if (kh[i] == 'L') cursor = pre[cursor];else {int l = cursor;int r = s[cursor].mac;if (l>r) l = l ^ r, r = l ^ r, l = l ^ r;if (NEXT[r]==n+1) {NEXT[pre[l]] = n + 1;cursor = pre[l];}
//          else if(l==1){
//              NEXT[0]
//          }else {NEXT[pre[l]] = NEXT[r];pre[NEXT[r]] = pre[l];cursor = NEXT[r];}}}for (int i = NEXT[0]; i <= n; i = NEXT[i]) {cout << S[i - 1];}
}

Correct Bracket Sequence Editor相关推荐

  1. Codeforces Round #350 (Div. 2) E. Correct Bracket Sequence Editor 栈 链表

    E. Correct Bracket Sequence Editor 题目连接: http://www.codeforces.com/contest/670/problem/E Description ...

  2. CF思维联系–CodeForces -224C - Bracket Sequence

    ACM思维题训练集合 A bracket sequence is a string, containing only characters "(", ")", ...

  3. CodeForces - 224C. Bracket Sequence (栈模拟)简单做法

    A bracket sequence is a string, containing only characters "(", ")", "[&quo ...

  4. 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.c ...

  5. C. Compressed Bracket Sequence

    William has a favorite bracket sequence. Since his favorite sequence is quite big he provided it to ...

  6. 02.08 Longest Regular Bracket Sequence

    最长的常规支架序列|断续器 (jxnu.edu.cn)https://acs.jxnu.edu.cn/problem/CF5C 描述: This is yet another problem deal ...

  7. Regular Bracket Sequence

    题目描述: A bracket sequence is called regular if it is possible to obtain correct arithmetic expression ...

  8. 【CodeForces】CF26B Regular Bracket Sequence

    题目地址: https://www.luogu.com.cn/problem/CF26B 题面翻译: 给出一个括号的序列,求最长的合法的子序列并输出. 原序列的长度≤106\le 10^6≤106 题 ...

  9. Educational Codeforces Round 4 C. Replace To Make Regular Bracket Sequence 栈

    C. Replace To Make Regular Bracket Sequence 题目连接: http://www.codeforces.com/contest/612/problem/C De ...

最新文章

  1. 爬虫-selenium初步学习与使用!
  2. 多线程并发:每个开发人员都应了解的内容
  3. CSS拉伸resize
  4. 半年时间能学会php吗_4个月真的能学会PHP吗
  5. java json float_java – Json解析问题(值自动更改为float)
  6. 51CTO大赛,欢迎投博主一票
  7. 从零学ELK系列(三):Centos安装Docker(超详细图文教程)
  8. python2和python3中的map()
  9. 计算机接口论文摘要,计算机接口技术论文
  10. 有赞百亿级日志系统架构设计
  11. 利用ajax实现excel报表导出(解决乱码问题)
  12. Map接口以及那些实现类
  13. html做出来发给别人链接,FINEBI仪表板的公共链接分享后,将分享的链接发给别人后无法在另外一台电脑上打开该链接网址。...
  14. OA系统实现(请假审批,mybatis)-1
  15. 竞品分析:阿里云 VS 腾讯云,AT的短兵相接
  16. Android四大组件和启动模式(面试总结)
  17. Windows 7驱动开发系列(四)--WDM模型介绍
  18. java 生成当天日期 昨天日期
  19. 让GIS三维可视化变得简单-初识Cesium
  20. 是时候为各式设备适配完善的输入支持了

热门文章

  1. 英飞凌TC3xx_MCMCAN(二)
  2. 使用Golang、Gin和React、esbuild开发的Blog
  3. 文件加密—巧用闪灵文件夹锁隐藏视频
  4. C++练习17:统计字符串中有多少个单词
  5. 钣金工艺流程可能各不相同,以下几点.
  6. java9 模块_了解Java 9模块
  7. 解决:ERROR: Cannot set priority of datanode process 10603
  8. android attributeset 工具类,android – 如何将AttributeSet传递给自定义视图
  9. 思维导图详细安装过程
  10. GMQ Wallet 跨时代产品,全面体现区块链数字资产价值