您的位置:首页 > 运维架构

【rope】bzoj1269 [AHOI2006]文本编辑器editor

2014-09-06 15:41 281 查看
维护一个字符串,支持以下操作:





主要就是 成段插入、成段删除、成段翻转。前两个操作很好通过rope实现。第三个操作也不难,维护两个rope,一个正向,一个反向,翻转时swap一下就行了。

rope教程: http://blog.csdn.net/iamzky/article/details/38348653
Code(Orz zky):

#include<cstdio>
#include<ext/rope>
using namespace std;
using namespace __gnu_cxx;
crope a,b,tmp;
int n,p,sz,len,res;
char op[21],s[2000001],r[2000001],c;
inline int getint(){res=0;c='*';while(c<'0'||c>'9')c=getchar();while(c>='0'&&c<='9'){res=res*10+(c-'0');c=getchar();}return res;}
int main()
{
scanf("%d",&n);
for(;n>0;n--)
{
scanf("%s",op);
if(op[0]=='M')p=getint();
else if(op[0]=='P')p--;
else if(op[0]=='N')p++;
else if(op[0]=='G'){putchar(a[p]);putchar('\n');}
else if(op[0]=='I')
{
sz=getint();
len=a.length();
for(int i=0;i<sz;i++){
do{s[i]=getchar();}while(s[i]=='\n');
r[sz-i-1]=s[i];
}
s[sz]=r[sz]='\0';
a.insert(p,s);
b.insert(len-p,r);
}
else if(op[0]=='D')
{
sz=getint();
len=a.length();
a.erase(p,sz);
b.erase(len-p-sz,sz);
}
else if(op[0]=='R')
{
sz=getint();
len=a.length();
tmp=a.substr(p,sz);
a=a.substr(0,p)+b.substr(len-p-sz,sz)+a.substr(p+sz,len-p-sz);
b=b.substr(0,len-p-sz)+tmp+b.substr(len-p,p);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: