我用了一点小技巧就是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;
}