E. Correct Bracket Sequence Editor
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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

 

题目链接:CodeForces 670E

我用了一点小技巧就是advance这个函数,可以让迭代器前进或者后退,以前只是了解过这个函数没想到现在能用到,另外还在list的结尾和开头都加了0防止越界的情况也方便反向遍历(否则要用返向迭代器想想都麻烦)。然后就可以进行模拟操作了,注意一下list的erase到底如何进行擦除,一般的for里it++会出问题的。

然后是括号如何判断,当然你首先要设一个当前位置迭代器list<char>::iterator now,然后看*now是'('还是')',若是前者,那肯定要向后遍历寻找,后者则相反,为什么?画图就明白了,不可能会反着来的(以前没意识到这点就以为这题十分麻烦)然后就是如何判断刚好是截取区间呢,对包括开始位置的括号种类cnt进行统计,若相等则可以break此时当前的迭代器位置就是闭区间[l,r],然后r++就可以使得区间变成[l,r),就可以进行删除操作了。

代码:

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<list>
#include<set>
#include<map>
using namespace std;
#define INF 0x3f3f3f3f
#define MM(x,y) memset(x,y,sizeof(x))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=500010;
char s[N],ops[N];
int cnt[2];
int main(void)
{
list<char>pos;
int n,m,p,i,j,l,r;
while (cin>>n>>m>>p)
{
pos.clear();
cnt[0]=cnt[1]=0;
cin>>s>>ops; pos.push_back('0');//开头放一个0
list<char>::iterator it,st,en,l,r,now;
for (i=0; i<n; ++i)
pos.push_back(s[i]);
pos.push_back('0');//结尾放一个0
now=pos.begin();
advance(now,p);//使用advance函数让其到初始位置p
for (i=0; i<m; ++i)
{
if(ops[i]=='L')
advance(now,-1);
else if(ops[i]=='R')
advance(now,1);
else
{
MM(cnt,0);
if(*now=='(')
{
l=r=now;
for (it=now; it!=pos.end(); ++it,++r)
{
if(*it=='(')
++cnt[0];
else if(*it==')')
++cnt[1];
if(cnt[0]==cnt[1])
break;
}
advance(r,1);
for (it=l; it!=r; )
pos.erase(it++);//erase的正确使用姿势
now=it;
if(*it=='0')//判断是否越界
advance(now,-1);
}
else
{
l=r=now;
++r;
for (it=now; it!=pos.begin(); --it,--l)
{
if(*it=='(')
++cnt[0];
else if(*it==')')
++cnt[1];
if(cnt[0]==cnt[1])
break;
}
for (it=l; it!=r; )
pos.erase(it++);
now=it;
if(*it=='0')
advance(now,-1);
}
}
}
st=pos.begin();
advance(st,1);
en=pos.end();
advance(en,-1);
for (it=st; it!=en; ++it)
cout<<*it;
cout<<endl;
}
return 0;
}

CodeForces 670E Correct Bracket Sequence Editor(list和迭代器函数模拟)的更多相关文章

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

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

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

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

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

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

  4. CodeForces 670E Correct Bracket Sequence Editor

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

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

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

  6. 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 ...

  7. 【31.93%】【codeforces 670E】Correct Bracket Sequence Editor

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  8. cf670E Correct Bracket Sequence Editor

    Recently Polycarp started to develop a text editor that works only with correct bracket sequences (a ...

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

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

随机推荐

  1. [Android Pro] PackageManager#getPackageSizeInfo (hide)

    referce to : http://www.baidufe.com/item/8786bc2e95a042320bef.html 计算Android App所占用d的手机内存(RAM)大小.App ...

  2. 【读书笔记】读《JavaScript高级程序设计-第2版》 - 非函数部分

    章节列表: 第08章:BOM 第09章:客户端检测 第10章:DOM 第11章:DOM2和DOM3 第12章:事件 第13章:表单脚本 第14章:错误处理与调试 第17章:Ajax和JSON第20章: ...

  3. 自定义view实现水波纹效果

    水波纹效果: 1.标准正余弦水波纹: 2.非标准圆形液柱水波纹: 虽说都是水波纹,但两者在实现上差异是比较大的,一个通过正余弦函数模拟水波纹效果,另外一个会运用到图像的混合模式(PorterDuffX ...

  4. 消息队列入门(三)JMS标准及实现

    >>消息中间件 消息中间件即Message-oriented middleware(MOM),消息中间件利用高效可靠的消息传递机制进行平台无关的数据交流,并基于数据通信来进行分布式系统的集 ...

  5. IOS杂谈

    1 IOS名称是iPhone Operating System 的缩写,原本这个系统名为iPhone OS,意思是iPhone 操作系统. 2 IOS的开发环境是Xcode.Xcode就成为了iPho ...

  6. BZOJ 3156: 防御准备 斜率优化DP

    3156: 防御准备 Description   Input 第一行为一个整数N表示战线的总长度. 第二行N个整数,第i个整数表示在位置i放置守卫塔的花费Ai. Output 共一个整数,表示最小的战 ...

  7. Codeforces Round #Pi (Div. 2) A. Lineland Mail 水

    A. Lineland MailTime Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/567/proble ...

  8. 【HTML5】Canvas画布

    什么是 Canvas? HTML5 的 canvas 元素使用 JavaScript 在网页上绘制图像. 画布是一个矩形区域,您可以控制其每一像素. canvas 拥有多种绘制路径.矩形.圆形.字符以 ...

  9. 移动零售批发行业新的技术特色-智能PDA手持移动扫描打印销售开单收银仪!!

    提起便利店或者超市,大家的第一印象一定是前台那个笨重的POS机和站在POS机后的收银员.传统的零售店中,笨重的POS机随处可见. 变革前,零售盘点多烦忧 一个顾客要结账,就需要通过POS机.小票打印机 ...

  10. POJ3694 Network(边双连通分量+缩点+LCA)

    题目大概是给一张图,动态加边动态求割边数. 本想着求出边双连通分量后缩点,然后构成的树用树链剖分+线段树去维护路径上的边数和..好像好难写.. 看了别人的解法,这题有更简单的算法: 在任意两点添边,那 ...