HYSBZ-1269 Text Editor editor (splay string interval operation)

Source: Internet
Author: User
Tags abstract definition strcmp

Text Editor editor
Time Limit: 10000MS Memory Limit: 165888KB 64bit IO Format: %lld &%llu

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

10Insert 13Balanced eertmove 2Delete 5NextInsert 7editorMove 0GetMove 11Rotate 4Get

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.

Analysis: Splay basic operation, for the move cursor operation, with a POS pointer to simulate the line; For insert operations, the first stretch operation moves the position of the POS to the location of the root node, moves the position of the pos+1 to the right subtree of the root node, and then inserts the string in its Zuozi. Same practice for delete and flip

#include <stdio.h>#include<string.h>#include<iostream>using namespacestd;#defineKey_value Ch[ch[root][1]][0]Const intMAXN =2*1024x768*1024x768+Ten;Const intINF =0x3f3f3f3f;intfa[maxn],ch[maxn][2],SZ[MAXN],REV[MAXN];CharKEY[MAXN];introot,tot1;intS[maxn],tot2;//memory pool, memory pool capacityintPos;CharSTR[MAXN];intN;voidNewNode (int&r,intPreChark) {    if(Tot2) r=s[tot2--]; Elser=++tot1; ch[r][0]=ch[r][1]=0; FA[R]=Pre; SZ[R]=1; REV[R]=0; KEY[R]=K;}voidUpdate_rev (intR) {    if(r==0)return; Swap (ch[r][0],ch[r][1]); REV[R]^=1;}voidPUSH_UP (intR) {Sz[r]=sz[ch[r][0]]+sz[ch[r][1]]+1;}voidPush_down (intR) {    if(Rev[r]) {Update_rev (ch[r][0]); Update_rev (ch[r][1]); REV[R]=0; }}voidBuild (int&x,intLintRintpre) {    if(L>r)return; intMid= (L+R)/2;    NewNode (X,pre,str[mid]); Build (ch[x][0],l,mid-1, x); Build (ch[x][1],mid+1, r,x); PUSH_UP (x);}voidInit () {pos=0; Root=tot1=tot2=0; ch[root][0]=ch[root][1]=sz[root]=rev[root]=fa[root]=0; NewNode (Root,0,' '); NewNode (ch[root][1],root,' '); //Build (key_value,1,n,ch[root][1]);PUSH_UP (ch[root][1]); Push_up (root);}voidRotate (intXintd) {    inty=Fa[x];    Push_down (y);    Push_down (x); ch[y][!d]=Ch[x][d]; FA[CH[X][D]]=y; if(Fa[y]) ch[fa[y]][ch[fa[y]][1]==y]=x; FA[X]=Fa[y]; CH[X][D]=y; Fa[y]=x; Push_up (y);}//stretching operation, adjust R to goal belowvoidSplay (intRintgoal)    {Push_down (R);  while(fa[r]!=goal) {        if(fa[fa[r]]==goal) {            //This problem has the reversal operation, needs first Push_down, in the judgment left and right childPush_down (Fa[r]);            Push_down (R); Rotate (r,ch[fa[r]][0]==R); }        Else        {            //This problem has the reversal operation, needs first Push_down, in the judgment left and right childPush_down (Fa[fa[r]);            Push_down (Fa[r]);            Push_down (R); inty=Fa[r]; intD= (ch[fa[y]][0]==y); //two different directions, then left-hand and then right-handed            if(ch[y][d]==r) {Rotate (R),!d);            Rotate (R,D); }            //two in the same direction, two in the same direction            Else{Rotate (y,d);            Rotate (R,D);    }}} push_up (r); if(goal==0) root=R;}intGet_kth (intRintk)    {Push_down (R); intt=sz[ch[r][0]]+1; if(t==k)returnR; if(t>k)returnGet_kth (ch[r][0],k); Else returnGet_kth (ch[r][1],k-t);}voidErase (intR) {    if(r) {s[++tot2]=R; Erase (ch[r][0]); Erase (ch[r][1]); }}voidUpdate_int (intLen) {splay (get_kth (Root,pos+1),0); Splay (get_kth (Root,pos+2), root); Build (Key_value,0, len-1, ch[root][1]); PUSH_UP (ch[root][1]); Push_up (root);}voidUpdate_del (intLen) {splay (get_kth (Root,pos+1),0); Splay (get_kth (Root,pos+len+2), root);    Erase (Key_value); Fa[key_value]=0; Key_value=0; PUSH_UP (ch[root][1]); Push_up (root);}voidUpdate_rot (intLen) {splay (get_kth (Root,pos+1),0); Splay (get_kth (Root,pos+len+2), root);    Update_rev (Key_value); PUSH_UP (ch[root][1]); Push_up (root);}intMain () {intx; Charop[ -]; scanf ("%d",&N);    Init ();  while(n--) {scanf ("%s", op); if(strcmp (OP,"Insert")==0) {scanf ("%d",&x);            GetChar ();            Gets (str); //cout<<str<<endl;update_int (x); }        Else if(strcmp (OP,"Delete")==0) {scanf ("%d",&x);        Update_del (x); }        Else if(strcmp (OP,"Rotate")==0) {scanf ("%d",&x);        Update_rot (x); }        Else if(strcmp (OP,"Get")==0) {printf ("%c\n", Key[get_kth (root,pos+2)]); }        Else if(strcmp (OP,"Move")==0) {scanf ("%d",&x); POS=x; }        Else if(strcmp (OP,"Next")==0) pos++; Else if(strcmp (OP,"Prev")==0) pos--; }    return 0;}

HYSBZ-1269 Text Editor editor (splay string interval operation)

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.