【NOIP2017提高A组模拟8.10】文本编辑器

来源:互联网 发布:工作总结工作计划知乎 编辑:程序博客网 时间:2024/06/05 08:13

Description

这里写图片描述

Input

第一行是初始内容
之后按照题目要求

Output

对于每个命令,按照要求输出

Sample Input

goodykc
11
I R u
I R l

L
L
L
L
R
D R
< R
D R
S

Sample Output

T
T
T
T
T
T
T
F
T
T
goodluck

Solution

毒瘤
显然可以用splay做,但是只有90分
100分必须线性做法
有一个比较简单的做法
左指针左边的维护一个栈,有指针右边的维护一个栈
中间的维护一个双向队列
是否翻转直接打标记
如果左指针跑到了有指针右边,就直接交换指针,并打交换标记
这不是唯一做法,但应该是细节最少最好做的做法

Code

#include<cstdio>#include<algorithm>#include<cstring>#define fo(i,a,b) for(int i=a;i<=b;i++)#define fd(i,a,b) for(int i=a;i>=b;i--)#define N 5010000using namespace std;char s[N];int a[N*2],b[2][N*2],re=0,rev=0,tot[2],l,r;int main(){    scanf("%s\n",s+1);    int n=strlen(s+1);    fo(i,1+N,n+N) a[i]=s[i-N];    l=1+N,r=n+N;    int ac;scanf("%d\n",&ac);    for(;ac;ac--)    {        char ch;scanf("%c",&ch);        if(ch=='<')        {            char W;scanf(" %c\n",&W);            int y=W=='L'?0:1;            if(re) y=1-y;            if(y==0)            {                if(tot[y]>0)                 {                    if(rev) a[++r]=b[0][tot[0]],tot[0]--;                    else a[--l]=b[0][tot[0]],tot[0]--;                    printf("T\n");                }                else printf("F\n");            }            else            {                if(tot[y]<n)                {                    if(tot[1]+tot[0]==n) re^=1;                    else                     {                        if(rev) b[1][++tot[1]]=a[l++];                        else b[1][++tot[1]]=a[r--];                    }                    printf("T\n");                }                else printf("F\n");            }        }        if(ch=='>')        {            char W;scanf(" %c\n",&W);            int y=W=='L'?0:1;            if(re) y=1-y;            if(y==0)            {                if(tot[y]<n)                 {                    if(tot[1]+tot[0]==n) re^=1;                    else                    {                        if(rev) b[0][++tot[0]]=a[r--];                        else b[0][++tot[0]]=a[l++];                    }                    printf("T\n");                }                else printf("F\n");            }            else            {                if(tot[y]>0)                {                    if(rev) a[--l]=b[1][tot[1]],tot[1]--;                    else a[++r]=b[1][tot[1]],tot[1]--;                    printf("T\n");                }                else printf("F\n");            }        }        if(ch=='I')        {            char W,c;scanf(" %c %c\n",&W,&c);            int y=W=='L'?0:1;            if(re) y=1-y;            if(y==0)            {                tot[0]++;b[0][tot[0]]=c;            }            else            {                if(rev) a[--l]=c;                else a[++r]=c;            }            n++;            printf("T\n");        }        if(ch=='D')        {            char W,c;scanf(" %c\n",&W);            int y=W=='L'?0:1;            if(re) y=1-y;            if(y==0)            {                if(r-l+1)                 {                    if(rev) r--;                    else l++;                    n--;                    printf("T\n");                }                else printf("F\n");            }            else            {                if(tot[1]>0)                {                    tot[1]--;                    printf("T\n");                }                else printf("F\n");            }        }        if(ch=='R')        {            scanf("\n");            if(!re) rev^=1,printf("T\n");            else printf("F\n");        }        if(ch=='S')        {            scanf("\n");            fo(i,1,tot[0]) printf("%c",b[0][i]);            if(rev) fd(i,r,l) printf("%c",a[i]);            else fo(i,l,r) printf("%c",a[i]);            fd(i,tot[1],1) printf("%c",b[1][i]);            printf("\n");        }    }}
原创粉丝点击