zoukankan      html  css  js  c++  java
  • [NOI2003],[AHOI2006]文本编辑器

    嘟嘟嘟


    [NOI2003]的其实就是一个板子……所以我就不说啥了。
    唯一需要注意的是读入字符(哎……):题中说“中间可能有空格,请忽略”的意思是要在程序里特判掉,不是不管他……
    输出的时候暴力中序遍历就行。


    然后[AHOI2006]的只是多了一个区间翻转,然后输出单个字符,其实还是板子。 只不过读入特别坑,推荐看luogu的讨论里的读入。 以及可能有些乱七八糟的操作,得手动忽略……
    [NOI2003] ```c++ #include #include #include #include #include #include #include #include #include #include using namespace std; #define enter puts("") #define space putchar(' ') #define Mem(a, x) memset(a, x, sizeof(a)) #define In inline typedef long long ll; typedef double db; const int INF = 0x3f3f3f3f; const db eps = 1e-8; const int maxn = 2e6 + 5; inline ll read() { ll ans = 0; char ch = getchar(), last = ' '; while(!isdigit(ch)) last = ch, ch = getchar(); while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar(); if(last == '-') ans = -ans; return ans; } inline void write(ll x) { if(x < 0) x = -x, putchar('-'); if(x >= 10) write(x / 10); putchar(x % 10 + '0'); }

    char s[20], a[maxn];
    int Cur = 0;
    struct Tree
    {
    int ch[2], fa;
    int siz; char c;
    }t[maxn];

    define ls t[now].ch[0]

    define rs t[now].ch[1]

    int root, tcnt = 0;
    int st[maxn], top = 0;

    In void _PrintTr(int now)
    {
    if(!now) return;
    printf("no:%d val:%c ls:%c rs:%c ", now, t[now].c, t[ls].c, t[rs].c);
    _PrintTr(ls); _PrintTr(rs);
    }

    In void init() //完全手动初始化……
    {
    t[root = ++tcnt].c = '('; t[tcnt].siz = 2; t[++tcnt].c = ')'; t[tcnt].siz = 1;
    t[root].ch[1] = tcnt; t[tcnt].fa = root;
    }

    In void pushup(int now)
    {
    t[now].siz = t[ls].siz + t[rs].siz + 1;
    }
    In void rotate(int x)
    {
    int y = t[x].fa, z = t[y].fa, k = (t[y].ch[1] == x);
    t[z].ch[t[z].ch[1] == y] = x; t[x].fa = z;
    t[y].ch[k] = t[x].ch[k ^ 1]; t[t[y].ch[k]].fa = y;
    t[x].ch[k ^ 1] = y; t[y].fa = x;
    pushup(y), pushup(x);
    }
    In void splay(int x, int s)
    {
    while(t[x].fa != s)
    {
    int y = t[x].fa, z = t[y].fa;
    if(z != s) rotate(((t[y].ch[0] == x) ^ (t[z].ch[0] == y)) ? x : y);
    rotate(x);
    }
    if(!s) root = x;
    }
    In int Find(int k)
    {
    int now = root;
    while(1)
    {
    if(t[ls].siz >= k) now = ls;
    else if(t[ls].siz + 1 == k) return now;
    else k -= (t[ls].siz + 1), now = rs;
    }
    }
    In int split(int L, int R)
    {
    int a = Find(L), b = Find(R + 2);
    splay(a, 0); splay(b, a);
    return t[root].ch[1];
    }
    In int build(int L, int R, int fa)
    {
    if(L > R) return 0;
    int mid = (L + R) >> 1, now = top ? st[top--] : ++tcnt;
    t[now].fa = fa;
    t[now].siz = 1; t[now].c = a[mid];
    ls = build(L, mid - 1, now);
    rs = build(mid + 1, R, now);
    pushup(now);
    return now;
    }
    In void update_in()
    {
    int n = read();
    char x = getchar(); while(x < 32 || x > 126) x = getchar();
    for(int i = 1; i <= n; ++i, x = getchar())
    {
    if(x < 32 || x > 126) {--i; continue;}
    a[i] = x;
    }
    int tp = build(1, n, 0);
    int a = Find(Cur + 1), b = Find(Cur + 2);
    splay(a, 0); splay(b, a);
    int now = t[root].ch[1];
    t[now].ch[0] = tp; t[tp].fa = now;
    pushup(now); pushup(root);
    }
    In void rec(int now)
    {
    if(!now) return;
    rec(ls); rec(rs);
    st[++top] = now;
    }
    In void update_del(int L, int R)
    {
    int now = split(L, R);
    rec(t[now].ch[0]);
    t[t[now].ch[0]].fa = 0; t[now].ch[0] = 0;
    pushup(now); pushup(root);
    }
    In void print(int now)
    {
    if(!now) return;
    print(ls);
    putchar(t[now].c);
    print(rs);
    }
    In void query(int L, int R)
    {
    int now = split(L, R);
    print(t[now].ch[0]);
    }

    int main()
    {
    int T = read();
    init();
    while(T--)
    {
    scanf("%s", s);
    if(s[0] == 'M') Cur = read();
    else if(s[0] == 'I') update_in();
    else if(s[0] == 'D')
    {
    int n = read();
    update_del(Cur + 1, Cur + n);
    }
    else if(s[0] == 'G')
    {
    int n = read();
    query(Cur + 1, Cur + n); enter;
    }
    else if(s[0] == 'P') Cur--;
    else Cur++;
    }
    return 0;
    }

    </br>
    [AHOI2006]
    ```c++
    #include<cstdio>
    #include<iostream>
    #include<cmath>
    #include<algorithm>
    #include<cstring>
    #include<cstdlib>
    #include<cctype>
    #include<vector>
    #include<stack>
    #include<queue>
    using namespace std;
    #define enter puts("") 
    #define space putchar(' ')
    #define Mem(a, x) memset(a, x, sizeof(a))
    #define In inline
    typedef long long ll;
    typedef double db;
    const int INF = 0x3f3f3f3f;
    const db eps = 1e-8;
    const int maxn = 2e6 + 5;
    inline ll read()
    {
      ll ans = 0;
      char ch = getchar(), last = ' ';
      while(!isdigit(ch)) last = ch, ch = getchar();
      while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
      if(last == '-') ans = -ans;
      return ans;
    }
    inline void write(ll x)
    {
      if(x < 0) x = -x, putchar('-');
      if(x >= 10) write(x / 10);
      putchar(x % 10 + '0');
    }
    
    char s[20], a[maxn];
    int Cur = 0;
    struct Tree
    {
      int ch[2], fa;
      int siz, rev; char c;
    }t[maxn];
    #define ls t[now].ch[0]
    #define rs t[now].ch[1]
    int root, tcnt = 0;
    int st[maxn], top = 0;
    
    In void _PrintTr(int now)
    {
      if(!now) return;
      printf("no:%d val:%c ls:%c rs:%c
    ", now, t[now].c, t[ls].c, t[rs].c);
      _PrintTr(ls); _PrintTr(rs);
    }
    
    In void init()
    {
      t[root = ++tcnt].c = '$'; t[tcnt].siz = 2;
      t[++tcnt].c = '$'; t[tcnt].siz = 1;
      t[root].ch[1] = tcnt; t[tcnt].fa = root;
    }
    
    In void c_rev(int now)
    {
      swap(ls, rs); t[now].rev ^= 1;
    }
    In void pushdown(int now)
    {
      if(t[now].rev)
        {
          if(ls) c_rev(ls);
          if(rs) c_rev(rs);
          t[now].rev = 0;
        }
    }
    In void pushup(int now)
    {
      t[now].siz = t[ls].siz + t[rs].siz + 1;
    }
    In void rotate(int x)
    {
      int y = t[x].fa, z = t[y].fa, k = (t[y].ch[1] == x);
      t[z].ch[t[z].ch[1] == y] = x; t[x].fa = z;
      t[y].ch[k] = t[x].ch[k ^ 1]; t[t[y].ch[k]].fa = y;
      t[x].ch[k ^ 1] = y; t[y].fa = x;
      pushup(y), pushup(x);
    }
    In void splay(int x, int s)
    {
      while(t[x].fa != s)
        {
          int y = t[x].fa, z = t[y].fa;
          if(z != s) rotate(((t[y].ch[0] == x) ^ (t[z].ch[0] == y)) ? x : y);
          rotate(x);
        }
      if(!s) root = x;
    }
    In int Find(int k)
    {
      int now = root;
      if(k > t[root].siz) {printf("ERR: %d %d
    ", k, t[root].siz); exit(0);}
      while(1)
        {
          pushdown(now);
          if(t[ls].siz >= k) now = ls;
          else if(t[ls].siz + 1 == k) return now;
          else k -= (t[ls].siz + 1), now = rs;
        }
    }
    In int split(int L, int R)
    {
      int a = Find(L), b = Find(R + 2);
      splay(a, 0); splay(b, a);
      pushdown(root); pushdown(t[root].ch[1]);
      return t[root].ch[1];
    }
    In int build(int L, int R, int fa)
    {
      if(L > R) return 0;
      int mid = (L + R) >> 1, now = top ? st[top--] : ++tcnt;
      t[now].fa = fa; t[now].rev = 0;
      t[now].siz = 1; t[now].c = a[mid];
      ls = build(L, mid - 1, now);
      rs = build(mid + 1, R, now);
      pushup(now);
      return now;
    }
    In void update_in()
    {
      int n = read();
      char x;
      for(int i = 1; i <= n; ++i)
        {
          x = getchar();
          while(x < 32 || x > 126)
    	{
    	  if(x == 10 || x == 13)
    	    {
    	      for(i = i; i <= n + 1; ++i) a[i] = 0;
    	      break;
    	    }
    	  x = getchar();
    	}
          a[i] = x;
        }
      int tp = build(1, n, 0);
      int a = Find(Cur + 1), b = Find(Cur + 2);
      splay(a, 0); splay(b, a);
      pushdown(root); pushdown(t[root].ch[1]);
      int now = t[root].ch[1];
      t[now].ch[0] = tp; t[tp].fa = now;
      pushup(now); pushup(root);
    }
    In void rec(int now)
    {
      if(!now) return;
      rec(ls); rec(rs);
      st[++top] = now;
    }
    In void update_del(int L, int R)
    {
      int now = split(L, R);
      rec(t[now].ch[0]);
      t[t[now].ch[0]].fa = 0; t[now].ch[0] = 0;
      pushup(now); pushup(root);
    }
    In void update_rev(int L, int R)
    {
      int now = split(L, R);
      c_rev(t[now].ch[0]);
      pushup(now); pushup(root);
    }
    In char query(int x)
    {
      int now = Find(x + 1); splay(now, 0);
      return t[root].c;
    }
    
    int main()
    {
      int T = read();
      init();
      while(T--)
        {
          scanf("%s", s);
          if(s[0] == 'M') Cur = read();
          else if(s[0] == 'I') update_in();
          else if(s[0] == 'D')
    	{
    	  int n = read();
    	  update_del(Cur + 1, Cur + n);
    	}
          else if(s[0] == 'R')
    	{
    	  int n = read();
    	  update_rev(Cur + 1, Cur + n);
    	}
          else if(s[0] == 'G')
    	{
    	  putchar(query(Cur + 1)); enter;
    	}
          else if(s[0] == 'P') Cur--;
          else if(s[0] == 'N') Cur++;
        }
      return 0;
    }
    
  • 相关阅读:
    linux 压力测试工具之ab
    docker save load export import的区别
    手把手教你打造高效的 Kubernetes 命令行终端
    K8S 中的容器编排和应用编排
    linux mount一个目录到另外一个目录
    linux sed命令详解
    各种安全证书间的关系及相关操作
    Linux Shell/Bash wildcard通配符、元字符、转义符使用
    vim打开多个文件、同时显示多个文件、在文件之间切换
    吉他演奏中的速度与节拍
  • 原文地址:https://www.cnblogs.com/mrclr/p/10180104.html
Copyright © 2011-2022 走看看