分析
我们发现,在这么多操作之中,R翻转是最难处理的。
那我们就先考虑如何将一个区间翻转。
splay?
这样可以拿到90分,如何拿到100分。
如何快速翻转一个区间,我们想到了双头队列,
对于翻转整个队列就是交换队头队尾。
我们将整个序列依次分为一个栈,左指针,一个双头队列,右指针,一个栈。
这样每一个操作就可以O(1)解决。
code
#include<cstdio>
#include<iostream>
#include<algorithm>
#include <cstring>
#include <string.h>
#include <cmath>
#include <math.h>
#define ll long long
#define N 4000003
#define db double
#define P putchar
#define G getchar
using namespace std;
int x,y,n,i,top1,top2;
int fx;
char z1[N*2],z2[N*2],s[N*2],ch,a,b,c,d;
int main()
{
freopen("editor.in","r",stdin);
freopen("editor.out","w",stdout);
ch=G();
x=2333;y=x-1;fx=1;
while(ch<33 || ch>126)ch=G();
while(33<=ch && ch<=126)s[++y]=ch,ch=G();
scanf("%d",&n);
while(n--)
{
ch=G();
while(ch!='I' && ch!='<' && ch!='>' && ch!='D' && ch!='S' && ch!='R')
ch=G();
if(ch=='I')
{
a=G();
while(a!='R' && a!='L')a=G();
b=G();
while(b<33 || b>126)b=G();
P('T'),P('\n');
if(a=='L')z1[++top1]=b;
else y+=fx,s[y]=b;
}
if(ch=='<')
{
a=G();
while(a!='R' && a!='L')a=G();
if(a=='L')
{
if(top1)
{
x-=fx;
s[x]=z1[top1--];
P('T');
}else P('F');
}
else
{
z2[++top2]=s[y];
y-=fx;
P('T');
}
P('\n');
}
if(ch=='>')
{
a=G();
while(a!='R' && a!='L')a=G();
if(a=='L')
{
z1[++top1]=s[x];
x+=fx;
P('T');
}
else
{
if(top2)
{
y+=fx;
s[y]=z2[top2--];
P('T');
}else P('F');
}
P('\n');
}
if(ch=='D')
{
a=G();
while(a!='R' && a!='L')a=G();
if(a=='L')x+=fx,P('T');else
{
if(top2)top2--,P('T');else P('F');
}
P('\n');
}
if(ch=='R')
{
swap(x,y);
fx=0-fx;
P('T'),P('\n');
}
if(ch=='S')
{
for(int i=1;i<=top1;i++)
P(z1[i]);
for(int i=x;i!=y;i=i+fx)
P(s[i]);
P(s[y]);
for(int i=top2;i;i--)
P(z2[i]);
P('\n');
}
}
}