zoukankan      html  css  js  c++  java
  • BZOJ1014 [JSOI2008]火星人

    Description

      火星人最近研究了一种操作:求一个字串两个后缀的公共前缀。比方说,有这样一个字符串:madamimadam,
    我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 8 9 10 11 字符 m a d a m i m a d a m 现在,
    火星人定义了一个函数LCQ(x, y),表示:该字符串中第x个字符开始的字串,与该字符串中第y个字符开始的字串
    ,两个字串的公共前缀的长度。比方说,LCQ(1, 7) = 5, LCQ(2, 10) = 1, LCQ(4, 7) = 0 在研究LCQ函数的过程
    中,火星人发现了这样的一个关联:如果把该字符串的所有后缀排好序,就可以很快地求出LCQ函数的值;同样,
    如果求出了LCQ函数的值,也可以很快地将该字符串的后缀排好序。 尽管火星人聪明地找到了求取LCQ函数的快速
    算法,但不甘心认输的地球人又给火星人出了个难题:在求取LCQ函数的同时,还可以改变字符串本身。具体地说
    ,可以更改字符串中某一个字符的值,也可以在字符串中的某一个位置插入一个字符。地球人想考验一下,在如此
    复杂的问题中,火星人是否还能够做到很快地求取LCQ函数的值。

    Input

      第一行给出初始的字符串。第二行是一个非负整数M,表示操作的个数。接下来的M行,每行描述一个操作。操
    作有3种,如下所示
    1、询问。语法:Qxy,x,y均为正整数。功能:计算LCQ(x,y)限制:1<=x,y<=当前字符串长度。
    2、修改。语法:Rxd,x是正整数,d是字符。功能:将字符串中第x个数修改为字符d。限制:x不超过当前字
    符串长度。
    3、插入:语法:Ixd,x是非负整数,d是字符。功能:在字符串第x个字符之后插入字符d,如果x=0,则在字
    符串开头插入。限制:x不超过当前字符串长度

    Output

      对于输入文件中每一个询问操作,你都应该输出对应的答案。一个答案一行。

    Sample Input

    madamimadam
    7
    Q 1 7
    Q 4 8
    Q 10 11
    R 3 a
    Q 1 7
    I 10 a
    Q 2 11

    Sample Output

    5
    1
    0
    2
    1

    HINT

    1、所有字符串自始至终都只有小写字母构成。

    2、M<=150,000

    3、字符串长度L自始至终都满足L<=100,000

    4、询问操作的个数不超过10,000个。

    对于第1,2个数据,字符串长度自始至终都不超过1,000

    对于第3,4,5个数据,没有插入操作。

    题解

    用平衡树维护字符串hash,查询时二分。

    不自然溢出的话在bzoj上会爆。

    代码:

    #include <algorithm>
    #include <cctype>
    #include <cstdio>
    #include <cstring>
    typedef long long LL;
    inline int readInt() {
      int ans = 0, c;
      while (!isdigit(c = getchar()));
      do ans = ans * 10 + c - '0';
      while (isdigit(c = getchar()));
      return ans;
    }
    const int base = 31;
    const int N = 100050;
    int pbase[N];
    inline int getHash(int h1, int h2, int l2) {
      return h1 * pbase[l2] + h2;
    }
    struct Splay{
      int son[N][2], fa[N];
      int v[N], len[N], hash[N];
      int root, cnt;
      inline void upd(int x) {
        int lc = son[x][0], rc = son[x][1];
        len[x] = len[lc] + len[rc] + 1;
        hash[x] = getHash(getHash(hash[lc], v[x], 1), hash[rc], len[rc]);
      }
      inline int dir(int x) { return son[fa[x]][1] == x; }
      inline void rotate(int x) {
        int f = fa[x], d = dir(x);
        if (son[f][d] = son[x][d ^ 1])
          fa[son[f][d]] = f;
        if (fa[x] = fa[f])
          son[fa[f]][dir(f)] = x;
        upd(son[fa[f] = x][d ^ 1] = f);
        upd(x);
      }
      void splay(int x, int p = 0) {
        for (; fa[x] != p; rotate(x))
          if (fa[fa[x]] != p) rotate(dir(fa[x]) == dir(x) ? fa[x] : x);
        if (!p) root = x;
      }
      void kth(int k, int x = -1) {
        if (x == -1) x = root;
        int y = fa[x];
        while (k != len[son[x][0]]) {
          if (k < len[son[x][0]]) {
            x = son[x][0];
          } else {
            k -= len[son[x][0]] + 1;
            x = son[x][1];
          }
        }
        splay(x, y);
      }
      void insert(int k, int w) {
        int p = ++cnt;
        v[p] = w;
        kth(k);
        if (!root) {
          root = p;
        } else {
          if (son[p][1] = son[root][1])
            fa[son[p][1]] = p;
          if (son[p][0] = root)
            fa[root] = p;
          son[root][1] = 0;
          upd(root);
          upd(root = p);
        }
      }
      inline void modify(int k, int w) {
        kth(k);
        v[root] = w;
        upd(root);
      }
      inline int query(int l, int r) {
        kth(r + 1);
        kth(l - 1, son[root][0]);
        return hash[son[son[root][0]][1]];
      }
    };
    Splay T;
    
    int n, m;
    char s[N];
    bool check(int a, int b, int l) {
      return T.query(a, a + l - 1) == T.query(b, b + l - 1);
    }
    int query(int a, int b) {
      int l = 0, r = n - std::max(a, b) - 1;
      while (l < r) {
        int mid = r + (l - r) / 2;
        if (check(a, b, mid)) l = mid;
        else r = mid - 1;
      }
      return l;
    }
    int main() {
      pbase[0] = 1;
      for (int i = 1; i < N; ++i) pbase[i] = (LL)pbase[i - 1] * base;
      scanf("%s", s + 1);
      n = strlen(s + 1) + 2;
      s[0] = s[n - 1] = 'z' + 1;
      for (int i = 0; i < n; ++i) T.insert(std::max(0, i - 1), s[i] - 'a');
      scanf("%d", &m);
      while (m--) {
        int x;
        while (!isalpha(*s = getchar()));
        if (*s == 'Q') {
          printf("%d
    ", query(readInt(), readInt()));
        } else if (*s == 'R') {
          x = readInt();
          while (!isalpha(*s = getchar()));
          T.modify(x, *s - 'a');
        } else {
          x = readInt();
          while (!isalpha(*s = getchar()));
          T.insert(x, *s - 'a');
          ++n;
        }
      }
      return 0;
    }
    

      

  • 相关阅读:
    YII框架学习(二)
    YII框架学习(一)
    valid number 判断字符串是否为有效数字
    leetcode Add Binary
    leetcode Minimum Path Sum
    leetcode Unique Paths II
    leetcode[61] Unique Paths
    leetcode[60] Rotate List
    leetcode Permutation Sequence
    leetcode Spiral Matrix II
  • 原文地址:https://www.cnblogs.com/y-clever/p/7468997.html
Copyright © 2011-2022 走看看