time limit per test2 seconds

memory limit per test256 megabytes

inputstandard input

outputstandard output

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:

command “R” — the cursor moves to the position 6 on the right;

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;

command “L” — the cursor moves to the position 4 on the left;

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 <algorithm> using namespace std; const int MAXN = 6e5; struct abcd
{
int l, r;
}; int n, m, pos,before[MAXN],d[MAXN];
char s1[MAXN],s2[MAXN];
abcd p[MAXN]; int main()
{
//freopen("F:\\rush.txt", "r", stdin);
scanf("%d%d%d", &n, &m, &pos);
scanf("%s", s1 + 1);
int now = 0;
for (int i = 1;i <= n;i++)
if (s1[i] == '(')
before[++now] = i;
else
{
d[before[now]] = i;
d[i] = before[now];
now--;
}
p[0].r = 1;
for (int i = 1; i <= n; i++)
p[i].l = i - 1, p[i].r = i + 1;
scanf("%s", s2 + 1);
for (int i = 1; i <= m; i++)
if (s2[i] == 'R')
pos = p[pos].r;
else
if (s2[i] == 'L')
pos = p[pos].l;
else
{
int left = min(d[pos], pos);
int right = max(d[pos], pos);
p[p[left].l].r = p[right].r;
p[p[right].r].l = p[left].l;
pos = p[right].r;
if (pos == n + 1)
pos = p[pos].l;
}
now = p[0].r;
while (now != n + 1)
{
putchar(s1[now]);
now = p[now].r;
}
puts("");
return 0;
}

【31.93%】【codeforces 670E】Correct Bracket Sequence Editor的更多相关文章

  1. CodeForces 670E Correct Bracket Sequence Editor(list和迭代器函数模拟)

    E. Correct Bracket Sequence Editor time limit per test 2 seconds memory limit per test 256 megabytes ...

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

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

  3. Codeforces 670E - Correct Bracket Sequence Editor - [线段树]

    题目链接:https://codeforces.com/contest/670/problem/E 题意: 给出一个已经匹配的括号串,给出起始的光标位置(光标总是指向某个括号). 有如下操作: 1.往 ...

  4. Codeforces 670E - Correct Bracket Sequence Editor - [链表]

    题目链接:https://codeforces.com/contest/670/problem/E 题意: 给出一个已经匹配的括号串,给出起始的光标位置(光标总是指向某个括号). 有如下操作: 1.往 ...

  5. Codeforces 670E - Correct Bracket Sequence Editor - [对顶栈]

    题目链接:https://codeforces.com/contest/670/problem/E 题意: 给出一个已经匹配的括号串,给出起始的光标位置(光标总是指向某个括号). 有如下操作: 1.往 ...

  6. CodeForces 670E Correct Bracket Sequence Editor

    链表,模拟. 写一个双向链表模拟一下过程. #pragma comment(linker, "/STACK:1024000000,1024000000") #include< ...

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

    题目链接: http://codeforces.com/contest/670/problem/E 题解: 用STL的list和stack模拟的,没想到跑的还挺快. 代码: #include<i ...

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

    题目链接:http://codeforces.com/contest/670/problem/E 给你n长度的括号字符,m个操作,光标初始位置是p,'D'操作表示删除当前光标所在的字符对应的括号字符以 ...

  9. 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 o ...

随机推荐

  1. Effective Modern C++:04智能指针

    裸指针有着诸多缺点:裸指针的声明中看不出它指向的是单个对象还是数组:裸指针的声明中也无法看出使用完它指向的对象后是否需要删除,也就是声明中看不出裸指针是否拥有其指向的对象:即使知道要析构裸指针指向的对 ...

  2. 安装软件时候出现"无效驱动器D"

    安装软件的时候,出现以下问题. 如图:  无效驱动器 原因是因为之前安装过这样的软件在H盘,后期更改没了H,所以出现了错误. 解决方案: 打开注册表,搜索软件的关键字如  vmware 删除错误路径即 ...

  3. 【JZOJ4817】【NOIP2016提高A组五校联考4】square

    题目描述 输入 输出 样例输入 3 4 1 1 0 1 0 1 1 0 0 1 1 0 5 1 1 2 3 2 1 3 2 3 2 3 4 1 1 3 4 1 2 3 4 样例输出 1 1 1 2 2 ...

  4. Java练习 SDUT-3849_分数四则运算

    分数四则运算 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 编写程序,实现两个分数的加减法 Input 输入包含多行数 ...

  5. 洛谷3067 BZOJ 2679题解(折半搜索)

    传送门 BZOJ传送门(权限题) 看到n小于20,就可以想到搜索 所有的数要么在集合a中,要么在集合b中,要么都不在 可是3^n复杂度会炸,我们考虑优化 可以利用折半搜索,将前面一半的所有可能情况与后 ...

  6. Libevent:1前言

    一:libevent概述: libevent是一个用来编写快速.可移植.非阻塞IO程序的库,它的设计目标是:可移植性.高效.可扩展性.便捷. libevent包含下列组件: evutil:对不同平台下 ...

  7. @codeforces - 444A@ DZY Loves Physics

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定一个 n 点 m 边的图,边有边权,点有点权. 找到一个连通 ...

  8. @loj - 2865@ 「IOI2018」狼人

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 在日本的茨城县内共有 N 个城市和 M 条道路.这些城市是根据人 ...

  9. 根据花瓶的侧面投影图,用Matlab绘制花瓶的三维立体图

    现有一花瓶侧面投影如图 问题: 1)    做出该花瓶三维立体图: 2)    计算其表面积:  计算其体积. 第一次参加数学建模,从来没有接触过Matlab语言,一上来就碰到这种数字图像处理的问题就 ...

  10. ODT模板

    struct node{ int l,r; mutable int v; node(int L,int R,int V):l(L),r(R),v(V){} inline bool operator & ...