zoukankan      html  css  js  c++  java
  • [BZOJ1269] [AHOI2006] 文本编辑器editor (splay)

    Description

      这些日子,可可不和卡卡一起玩了,原来可可正废寝忘食的想做一个简单而高效的文本编辑器。你能帮助他吗?为了明确任务目标,可可对“文本编辑器”做了一个抽象的定义:  

      文本:由0个或多个字符构成的序列。这些字符的ASCII码在闭区间[32, 126]内,也就是说,这些字符均为可见字符或空格。光标:在一段文本中用于指示位置的标记,可以位于文本的第一个字符之前,文本的最后一个字符之后或文本的某两个相邻字符之间。文本编辑器:为一个可以对一段文本和该文本中的一个光标进行如下七条操作的程序。如果这段文本为空,我们就说这个文本编辑器是空的。

      编写一个程序:
       建立一个空的文本编辑器。
       从输入文件中读入一些操作指令并执行。
       对所有执行过的GET操作,将指定的内容写入输出文件。

    Input

      输入文件中第一行是指令条数N,以下是需要执行的N个操作。除了回车符之外,输入文件的所有字符的ASCII码都在闭区间[32, 126]内。且行尾没有空格。

    Output

      依次对应输入文件中每条GET指令的输出,不得有任何多余的字符。

    Sample Input

    10
    Insert 13
    Balanced eert
    Move 2
    Delete 5
    Next
    Insert 7
    editor
    Move 0
    Get
    Move 11
    Rotate 4
    Get

    Sample Output

    B
    t

    HINT

      对输入数据我们有如下假定:
       MOVE操作不超过50 000个,INSERT、DELETE和ROTATE操作作的总个数不超过6 000,GET操作不超过20 000个,PREV和NEXT操作的总个数不超过20 000。
       所有INSERT插入的字符数之和不超过2M(1M=1 024*1 024)。
       DELETE操作、ROTATE操作和GET操作执行时光标后必然有足够的字符。MOVE、PREV、NEXT操作不会把光标移动到非法位置。
       输入文件没有错误。

    Source

      鸣谢seter重新制作数据

    Solution

      有没有觉得和某一道题很像?

      ——为什么我觉得rotate应该是reverse?

      1 #include <bits/stdc++.h>
      2 using namespace std;
      3 struct spaly
      4 {
      5     char key;
      6     int siz, fa, c[2], rev;
      7 }a[2100005];
      8 char inss[2100005];
      9 int ptot = 2;
     10  
     11 void push_up(int k)
     12 {
     13     a[k].siz = a[a[k].c[0]].siz + a[a[k].c[1]].siz + 1;
     14 }
     15  
     16 void push_down(int k)
     17 {
     18     if(a[k].rev)
     19     {
     20         swap(a[k].c[0], a[k].c[1]), a[k].rev = 0;
     21         a[a[k].c[0]].rev ^= 1, a[a[k].c[1]].rev ^= 1;
     22     }
     23 }
     24  
     25 void rotate(int &k, int x)
     26 {
     27     int y = a[x].fa, z = a[y].fa;
     28     int dy = a[y].c[1] == x, dz = a[z].c[1] == y;
     29     push_down(y);
     30     if(k == y) k = x, a[x].fa = z;
     31     else a[z].c[dz] = x, a[x].fa = z;
     32     a[y].c[dy] = a[x].c[!dy], a[a[x].c[!dy]].fa = y;
     33     a[x].c[!dy] = y, a[y].fa = x;
     34     push_up(y);
     35 }
     36  
     37 void splay(int &k, int x)
     38 {
     39     push_down(x);
     40     while(k != x)
     41     {
     42         int y = a[x].fa, z = a[y].fa;
     43         if(k != y)
     44             if(a[y].c[1] == x ^ a[z].c[1] == y) rotate(k, x);
     45             else rotate(k, y);
     46         rotate(k, x);
     47     }
     48     push_up(x);
     49 }
     50  
     51 int find(int k, int x)
     52 {
     53     if(!k) return 0;
     54     push_down(k);
     55     if(x <= a[a[k].c[0]].siz) return find(a[k].c[0], x);
     56     if(x == a[a[k].c[0]].siz + 1) return k;
     57     return find(a[k].c[1], x - a[a[k].c[0]].siz - 1);
     58 }
     59  
     60 void build(int &k, int fa, int i, int m)
     61 {
     62     if(i == m) return;
     63     k = ++ptot, a[k].fa = fa, a[k].key = inss[i];
     64     build(a[k].c[1], k, i + 1, m);
     65     push_up(k);
     66 }
     67  
     68 int main()
     69 {
     70     int n, m, root = 1, pos = 0, ctot = 0;
     71     char op[10];
     72     scanf("%d", &n);
     73     a[1].c[1] = a[1].siz = 2, a[2].siz = a[2].fa = 1;
     74     a[1].key = a[2].key = ' ';
     75     while(n--)
     76     {
     77         scanf("%s", op);
     78         if(op[0] == 'M') scanf("%d", &pos);
     79         if(op[0] == 'I')
     80         {
     81             scanf("%d", &m), ctot += m;
     82             for(int i = 0; i < m; i++)
     83             {
     84                 inss[i] = getchar();
     85                 if(inss[i] < 32 || inss[i] > 126) i--;
     86             }
     87             splay(root, find(root, pos + 1));
     88             splay(a[root].c[1], find(root, pos + 2));
     89             build(a[a[root].c[1]].c[0], a[root].c[1], 0, m);
     90             push_up(a[root].c[1]), push_up(root);
     91         }
     92         if(op[0] == 'D')
     93         {
     94             scanf("%d", &m), m = min(m, ctot - pos);
     95             splay(root, find(root, pos + 1));
     96             splay(a[root].c[1], find(root, pos + m + 2));
     97             a[a[root].c[1]].c[0] = 0, ctot -= m;
     98             push_up(a[root].c[1]), push_up(root);
     99         }
    100         if(op[0] == 'R')
    101         {
    102             scanf("%d", &m), m = min(m, ctot - pos);
    103             splay(root, find(root, pos + 1));
    104             splay(a[root].c[1], find(root, pos + m + 2));
    105             a[a[a[root].c[1]].c[0]].rev ^= 1;
    106         }
    107         if(op[0] == 'G')
    108         {
    109             splay(root, find(root, pos + 2));
    110             printf("%c
    ", a[root].key);
    111         }
    112         if(op[0] == 'P') pos--;
    113         if(op[0] == 'N') pos++;
    114     }
    115     return 0;
    116 }
    View Code
  • 相关阅读:
    Spring Boot(二十):使用spring-boot-admin对spring-boot服务进行监控
    spring Boot(十九):使用Spring Boot Actuator监控应用
    Spring Boot(十八):使用Spring Boot集成FastDFS
    Spring Boot(十七):使用Spring Boot上传文件
    Spring Boot(十六):使用Jenkins部署Spring Boot
    Spring Boot(十五):spring boot+jpa+thymeleaf增删改查示例
    Spring Boot(十四):spring boot整合shiro-登录认证和权限管理
    ubuntu18.04使用vscode报pylint is not install错误
    处理程序“PageHandlerFactory-Integrated”在其模块列表中有一个错误模块“Manag
    在SQL Server中,为何都建议禁止 VIA 协议,VIA协议具体内容是什么?
  • 原文地址:https://www.cnblogs.com/CtrlCV/p/5356772.html
Copyright © 2011-2022 走看看