[Rope] bzoj1269 [ahoi2006] Text Editor

Source: Internet
Author: User

To maintain a string, you can perform the following operations:

 
 
It mainly refers to segment insertion, segment deletion, and segment flip. The first two operations are implemented through rope. The third operation is not difficult. Maintain two ropes, one forward and the other backward. When you flip, you just need to swap. Rope Tutorial: http://blog.csdn.net/iamzky/article/details/38348653 code (orz zky ):
 1 #include<cstdio> 2 #include<ext/rope> 3 using namespace std; 4 using namespace __gnu_cxx; 5 crope a,b,tmp; 6 int n,p,sz,len,res; 7 char op[21],s[2000001],r[2000001],c; 8 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;} 9 int main()10 {11     scanf("%d",&n);12     for(;n>0;n--)13       {14           scanf("%s",op);15           if(op[0]==‘M‘)p=getint();16           else if(op[0]==‘P‘)p--;17         else if(op[0]==‘N‘)p++;18           else if(op[0]==‘G‘){putchar(a[p]);putchar(‘\n‘);}19           else if(op[0]==‘I‘)20             {21                 sz=getint();22                 len=a.length();23                 for(int i=0;i<sz;i++){24                   do{s[i]=getchar();}while(s[i]==‘\n‘);25                   r[sz-i-1]=s[i];26                 }27             s[sz]=r[sz]=‘\0‘;28             a.insert(p,s);29             b.insert(len-p,r);30             }31           else if(op[0]==‘D‘)32             {33               sz=getint();34             len=a.length();35             a.erase(p,sz);36             b.erase(len-p-sz,sz);37             }38           else if(op[0]==‘R‘)39             {40                 sz=getint();41             len=a.length();42             tmp=a.substr(p,sz);43             a=a.substr(0,p)+b.substr(len-p-sz,sz)+a.substr(p+sz,len-p-sz);44             b=b.substr(0,len-p-sz)+tmp+b.substr(len-p,p);45             }46       }47     return 0;48 }

 

[Rope] bzoj1269 [ahoi2006] Text Editor

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.