[bzoj1269] [AHOI2006 Text editor editor] (splay template problem)

Source: Internet
Author: User
Tags abstract definition

Description

These days, can not play with Kaka, originally cocoa is sleeping and sleeping to do a simple and efficient text editor. Can you help him? To clarify the task target, Cocoa makes an abstract definition of the text editor: text: A sequence of 0 or more characters. The ASCII codes for these characters are within the closed interval [32, 126], meaning that these characters are visible characters or spaces. Cursor: A marker in a text that indicates a position, which can precede the first character of the text, after the last character of the text, or between some two adjacent characters of the text. Text Editor: A program that can perform the following seven actions on a piece of text and a cursor in the text. If this text is empty, we will say that the text editor is empty. Write a program:? Create an empty text editor. Read some operation instructions from the input file and execute.? Writes the specified content to the output file for all performed get operations.

Input

The first line in the input file is the number of instruction bars N, and here are the N operations that need to be performed. In addition to the carriage return character, the ASCII code for all characters of the input file is within the closed interval [32, 126]. And there are no spaces at the end of the line.

Output

The output of each get instruction in the input file, in turn, must not have any extra characters.

Sample Input
 2 57 0    4 Get
Sample Output
Bt
HINT

We have the following assumptions about the input data:? Move operation no more than 50 000, INSERT, delete and rotate operation of the total number of not more than 6 000,get operation not more than 20 000, prev and next operation of the total number of not more than 20 000.? The sum of the characters of all insert inserts does not exceed 2M (1m=1 024*1 024). Delete operations, rotate operations, and get operations must have enough characters after the cursor to execute. Move, PREV, next do not move the cursor to an illegal position. The input file has no errors.

Solution

The template is as short as possible here. The non-recursive code that can be replaced by the loop has been replaced, and the efficiency is generally the same.

To implement the operation:
1. (known) Move K: Move cursor to target, initial to 0
2. (known) prev: Cursor moves forward one character
3. (known) Next: Move the cursor back one character
4.insert N S: Inserts a string of length n after the cursor s cursor position unchanged
5.delete N Deletes the n characters after the cursor, and the cursor position is unchanged
6.rotate N reverses the n characters after the cursor, and the cursor position is unchanged
7.get output cursor After one character, the cursor position is unchanged

#include <cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespaceStd;inlineintRin () {intx=0, C=getchar (), f=1;  for(;c< -|| C> $; c=GetChar ())if(! (c^ $)) f=-1;  for(;c> -&&c< -; c=GetChar ()) x= (x<<1) + (x<<3) +c- -; returnx*F;}intN;Chara[1<< A],b[Ten];structnode{Node*ch[2],*p; CharVintS,rev; InlinevoidRelax () {if(rev) ch[0]->revit (), ch[1]->revIt (); Rev=0; } InlinevoidPu () {s=ch[0]->s+ch[1]->s+1;} InlineBOOLD () {returnp->ch[1]== This;} InlinevoidSETC (Node *a,intd) {ch[d]=a;a->p= This;} InlinevoidRevIt () {rev^=1; Swap (ch[0],ch[1]);}}; Node*NULL=Newnode (); node*root=NULL; inline node*newnode (Node *f,Charval) {Node*re=Newnode (); Re->s=1; Re->v=Val; Re->rev=0; Re->ch[0]=re->ch[1]=NULL; Re->p=F; returnre;} InlinevoidRot (node*&N) {Node*p=o->p; P-relax (); o-relax (); BOOLD=o->d (); P-&GT;P-&GT;SETC (o,p->d ()); P-&GT;SETC (o->ch[!d],d); o-&GT;SETC (p,!d); P->pu ();o->Pu (); if(p==root) root=o;} InlinevoidSplay (node*o,node*f) {     while(o->p!=f) {        if(o->p->p==f) Rot (o); Elseo->d () ^o->p->d ()? (Rot (o), Rot (o)):(rot (o->p), Rot (o)); o-Pu (); }}node*build (intLintR) {    if(L>r)return NULL; intMid= (l+r) >>1; Node*o=NewNode (O,a[mid]); o-&GT;SETC (Build (l,mid-1),0); o-&GT;SETC (Build (mid+1, R),1); o-Pu (); returno;}voidDel (node* &x) {    if(x->ch[0]!=NULL) del (x->ch[0]); if(x->ch[1]!=NULL) del (x->ch[1]); Deletex;} Node*KTH (Node *x,intk) {     for(Node *x=root;;) {x-relax (); if(k<=x->ch[0]->s) x=x->ch[0]; Else{k-=x->ch[0]->s; if(! (k^1))returnx; X=x->ch[1];k--; }    }}intMain () {n=Rin (); Root=build (0,1); Root->p=NULL;  for(intx,t=0, i=1; i<=n;i++) {scanf ("%s", B); if(b[0]=='M') T=Rin (); Else if(b[0]=='I') {x=Rin ();  while((a[0]=getchar ()) = ='\ n'|| a[0]=='\ r'); if(x>1) gets (A +1); Splay (KTH (root,t+1),NULL); Splay (KTH (root,t+2), root); Root->ch[1]-&GT;SETC (Build (0, X-1),0); Root->ch[1]->Pu (); Root-Pu (); }        Else if(b[0]=='D') {x=Rin (); Splay (KTH (root,t+1),NULL); Splay (KTH (root,t+x+2), root); Del (Root->ch[1]->ch[0]); Root->ch[1]->ch[0]=NULL; Root->ch[1]->Pu (); Root-Pu (); }        Else if(b[0]=='R') {x=Rin (); Splay (KTH (root,t+1),NULL); Splay (KTH (root,t+x+2), root); Root->ch[1]->ch[0]->revIt (); Root->ch[1]->Pu (); Root-Pu (); }        Else if(b[0]=='G') {splay (kth (root,t+2),NULL); printf ("%c\n",root->v); }        Else if(b[0]=='P') t--; Elset++; }}

[bzoj1269] [AHOI2006 Text editor editor] (splay template problem)

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.