2 seconds
256 megabytes
standard input
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?
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.
Print the correct bracket sequence, obtained as a result of applying all operations to the initial sequence.
8 4 5
(())()()
RDLD
()
12 5 3
((()())(()))
RRDLD
(()(()))
8 8 8
(())()()
LLLLLLDD
()()
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 ().
题目链接: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;
}