bzoj1269[AHOI2006]文本编辑器editor

bzoj1269[AHOI2006]文本编辑器editor

题意:

维护一个字符串,支持插入,删除,翻转操作。

题解:

C++有个库里面有个容器叫rope,可以实现可持久化平衡树,然而本题只要它的插入、删除、截取字符串功能就行了,翻转怎么办?维护一个倒序的rope即可。

代码:

 1 #include <cstdio>
 2 #include <algorithm>
 3 #include <cstring>
 4 #include <ext/rope>
 5 #define maxn 2000000
 6 #define inc(i,j,k) for(int i=j;i<=k;i++)
 7 using namespace std;
 8 using namespace __gnu_cxx;
 9 
10 inline int read(){
11     char ch=getchar(); int f=1,x=0;
12     while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}
13     while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
14     return f*x;
15 }
16 rope <char> l,r,tmp; char ls[maxn],rs[maxn],opt[10]; int n,now;
17 int main(){
18     n=read();
19     inc(i,1,n){
20         scanf("%s",opt);
21         if(opt[0]=='M')now=read(); if(opt[0]=='P')now--;
22         if(opt[0]=='N')now++; if(opt[0]=='G')printf("%c\n",l[now]);
23         if(opt[0]=='D'){int x=read(),len=l.length(); l.erase(now,x); r.erase(len-now-x,x);}
24         if(opt[0]=='R'){
25             int x=read(),len=l.length(); tmp=l.substr(now,x);
26             l=l.substr(0,now)+r.substr(len-now-x,x)+l.substr(now+x,len-now-x);
27             r=r.substr(0,len-now-x)+tmp+r.substr(len-now,now);
28         }
29         if(opt[0]=='I'){
30             int x=read(),len=l.length();
31             inc(i,0,x-1){
32                 ls[i]=getchar(); while(ls[i]<32||ls[i]>126)ls[i]=getchar(); rs[x-i-1]=ls[i];
33             }
34             ls[x]=rs[x]=0; l.insert(now,ls); r.insert(len-now,rs);
35         }
36     }
37 }

 

20160620

转载于:https://www.cnblogs.com/YuanZiming/p/5701053.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值