CF670E.Correct Bracket Sequence Editor

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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?

最近,Polycarp 开始开发一款仅支持合法括号序列(简称 CBS)的文本编辑器。

注意:若能在该括号序列中插入若干个 "+" 和 "1" 使其成为合法的数学表达式,则称该括号序列为合法的。例如,"(())()"、"()" 和 "(())(()))" 是合法的;而 ")("、"(()" 和 "(())(" 则不合法。在任意一个 CBS 中,每个括号均有唯一配对的括号。例如,在 "(())(()))" 中:

  • 第 1 个括号与第 8 个括号配对,
  • 第 2 个括号与第 3 个括号配对,
  • 第 3 个括号与第 2 个括号配对,
  • 第 4 个括号与第 7 个括号配对,
  • 第 5 个括号与第 6 个括号配对,
  • 第 6 个括号与第 5 个括号配对,
  • 第 7 个括号与第 4 个括号配对,
  • 第 8 个括号与第 1 个括号配对。

目前 Polycarp 的编辑器仅支持 CBS 上的三种操作。编辑器中的光标始终位于某个括号所在位置(而非两个括号之间的空隙)。支持的操作如下:

  • «L» — 将光标向左移动一位,
  • «R» — 将光标向右移动一位,
  • «D» — 删除光标所在位置的括号、其配对括号,以及二者之间的所有括号(即:删除以光标所在括号和其配对括号为端点的子串)。

执行操作 «D» 后,光标将移至右侧最近的未被删除的括号处(当然,仅考虑尚未被删除的括号)。若右侧不存在这样的括号(即 CBS 的后缀被全部删除),则光标移至左侧最近的未被删除的括号处(同样仅考虑尚未被删除的括号)。

下方图片展示了若干次操作 «D» 的具体效果示例:

所有非法操作(如光标移出 CBS 边界、删除整个 CBS 等)均不被 Polycarp 的编辑器支持。

Polycarp 对自己的开发成果非常自豪,你能实现该编辑器的功能吗?

输入格式

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.

第一行包含三个正整数 nn、mm 和 pp(2 ≤ n ≤ 500 0002 \leq n \leq 500\,000,1 ≤ m ≤ 500 0001 \leq m \leq 500\,000,1 ≤ p ≤ n1 \leq p \leq n)——分别表示合法括号序列中括号的总数、操作次数以及光标的初始位置。序列中的位置从左到右编号,起始编号为 1。保证 nn 为偶数。

接下来是一行长度为 nn 的字符串,仅由字符 ( 和 ) 组成,构成一个合法括号序列。

随后是一行长度为 mm 的字符串,仅由字符 L、R 和 D 组成——表示一系列操作。这些操作将按顺序依次执行,从第一个操作开始,直至最后一个操作结束。保证所给的操作永远不会使光标移出括号序列的范围,且在所有操作执行完毕后,括号序列非空。

输出格式

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

输出对初始序列应用所有操作后得到的正确括号序列。

输入输出样例

  • 输入#1

    8 4 5
    (())()()
    RDLD

    输出#1

    ()
  • 输入#2

    12 5 3
    ((()())(()))
    RRDLD

    输出#2

    (()(()))
  • 输入#3

    8 8 8
    (())()()
    LLLLLLDD

    输出#3

    ()()

说明/提示

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

在第一个样例中,光标初始位于位置 5。考虑编辑器执行的操作:

  1. 命令 “R” — 光标向右移动至位置 6;
  2. 命令 “D” — 删除从位置 5 到位置 6 的括号。此后 CBS 变为 (())(),光标位于位置 5;
  3. 命令 “L” — 光标向左移动至位置 4;
  4. 命令 “D” — 删除从位置 1 到位置 4 的括号。此后 CBS 变为 (),光标位于位置 1。

因此,答案为 ()。

输入解题思路,AI测评打分。不知道怎么写?

首页