借用大牛的一张图片:模拟
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<memory.h>
#include<cstdlib>
#include<vector>
using namespace std;
const int MAXN=;
const double eps=1e-;
const int inf=0x3f3f3f3f; string s,ss,cs;
vector<int>pos;
int mode;
int now,m;
void L()
{
if(now>)
now--;
} void R()
{
if(now<ss.length())
now++;
} void S()
{
mode=!mode;
pos.clear();
}
void C()
{
pos.push_back(now);
if(pos.size()==)
{
sort(pos.begin(),pos.end());
cs=ss.substr(pos[],pos[]-pos[]);
pos.clear();
}
}
void D()
{
if(pos.size())
{
pos.push_back(now);
sort(pos.begin(),pos.end());
ss.erase(ss.begin()+pos[],ss.begin()+pos[]);
now=pos[];
pos.clear();
}
else if(now<ss.length())
{
ss.erase(ss.begin()+now,ss.begin()+now+);
}
} void B()
{
if(now>)
{
ss.erase(ss.begin()+now-,ss.begin()+now);
now--;
}
pos.clear();
} void V()
{
if(cs.length())
{
if(!mode)
{
if(ss.length()+cs.length()<=m)
{
int len=cs.length();
for(int i=; i<len; i++,now++)
{
ss.insert(ss.begin()+now,cs[i]);
}
}
}
else
{
if(now+cs.length()+<=m)
{
int len1=cs.length();
int len2=ss.length();
for(int i=; i<len1; i++,now++)
{
if(now>=len2)
ss.insert(ss.begin()+now,cs[i]);
else
ss[now]=cs[i];
}
}
}
}
pos.clear();
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
cin>>m>>s;
int len=s.length();
mode=;
now=;
ss.clear();
cs.clear();
pos.clear();
for(int i=; i<len&&m; i++)
{
if(s[i]=='L')
L();
else if(s[i]=='R')
R();
else if(s[i]=='C')
C();
else if(s[i]=='V')
V();
else if(s[i]=='D')
D();
else if(s[i]=='B')
B();
else if(s[i]=='S')
S();
else
{
if(mode&&now<ss.length())
{
ss[now++]=s[i];
}
else if(ss.length()<m)
{
ss.insert(ss.begin()+now,s[i]);
now++;
}
pos.clear();
}
}
if(ss.size())
{
cout<<ss<<endl;
}
else
cout<<"NOTHING"<<endl;
}
return ;
}