zoukankan      html  css  js  c++  java
  • [BZOJ1014][JSOI2008]火星人prefix splay+二分+hash

    1014: [JSOI2008]火星人prefix

    Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 8083  Solved: 2554 [Submit][Status][Discuss]

    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个数据,没有插入操作。

    用splay维护hash,二分答案查询

      1 #include<iostream>
      2 #include<cstring>
      3 #include<cstdio>
      4 #include<cstdlib>
      5 #include<cmath>
      6 #include<algorithm>
      7 #define mod 9875321
      8 using namespace std;
      9 struct data {
     10     int s[2];
     11     int fa,size;
     12     int v,val;
     13 }t[150005];
     14 int p[150005];
     15 int cnt=0,rt;
     16 char ch[150005];
     17 int m;
     18 int len;
     19 inline void pushup(int x) {
     20     int lc=t[x].s[0],rc=t[x].s[1];
     21     t[x].size=t[lc].size+t[rc].size+1;
     22     t[x].v=t[lc].v+(long long)t[x].val*p[t[lc].size]%mod+(long long)p[t[lc].size+1]*t[rc].v%mod;
     23     t[x].v%=mod;
     24 }
     25 inline void rorate(int x,int &k) {
     26     int y=t[x].fa,z=t[y].fa,l,r;
     27     if(t[y].s[0]==x) l=0;else l=1;
     28     r=l^1;
     29     if(y==k) k=x;
     30     else{if(t[z].s[0]==y) t[z].s[0]=x;else t[z].s[1]=x;}
     31     t[x].fa=z;t[y].fa=x;t[y].s[l]=t[x].s[r];t[t[x].s[r]].fa=y;t[x].s[r]=y;
     32     pushup(y);pushup(x);
     33 }
     34 inline void splay(int x,int &k) {
     35     while(x!=k) {
     36         int y=t[x].fa,z=t[y].fa;
     37         if(y!=k) {
     38             if(t[y].s[0]==x^t[z].s[0]==y) rorate(x,k);
     39             else rorate(y,k);
     40         }
     41         rorate(x,k);
     42     }
     43 }
     44 inline int find(int now,int x) {
     45     if(t[t[now].s[0]].size+1<x) return find(t[now].s[1],x-t[t[now].s[0]].size-1);
     46     else if(t[t[now].s[0]].size+1>x) return find(t[now].s[0],x);
     47     else return now;
     48 }
     49 inline void insert(int x,int val) {
     50     int t1=find(rt,x),t2=find(rt,x+1);
     51     splay(t1,rt);splay(t2,t[rt].s[1]);
     52     t[t2].s[0]=++cnt;t[cnt].val=val;t[cnt].fa=t2;
     53     pushup(cnt);pushup(t2);pushup(t1);
     54 }
     55 inline void change(int now,int x,int val) {
     56     if(t[t[now].s[0]].size+1<x) change(t[now].s[1],x-t[t[now].s[0]].size-1,val);
     57     else if(t[t[now].s[0]].size+1>x) change(t[now].s[0],x,val);
     58     else t[now].val=val;
     59     pushup(now);
     60 }
     61 inline int query(int x,int y) {
     62     int t1=find(rt,x-1);int t2=find(rt,y+1);
     63     splay(t1,rt);splay(t2,t[rt].s[1]);
     64     return t[t[t[rt].s[1]].s[0]].v;
     65 }
     66 inline bool check(int now,int x,int y) {
     67     if(x+now-1>len) return 0;
     68     if(y+now-1>len) return 0;
     69     return query(x,x+now-1)==query(y,y+now-1);
     70 }
     71 int main() {
     72     p[0]=1;for(int i=1;i<=150004;i++)p[i]=p[i-1]*27%mod;
     73     scanf("%s",ch+1);
     74     len=strlen(ch+1);
     75     t[1].size=1;pushup(1);
     76     for(int i=1;i<=len;i++) {
     77         t[i].fa=i+1;t[i+1].s[0]=i;t[i+1].val=ch[i]-'a'+1;pushup(i+1);
     78     }
     79     t[len+1].fa=len+2;t[len+2].s[0]=len+1;t[len+2].val=0;rt=len+2;pushup(len+2);
     80     cnt=len+2;
     81     len++;
     82     scanf("%d",&m);
     83     for(int i=1;i<=m;i++) {
     84         char h[10];
     85         scanf("%s",h);
     86         if(h[0]=='Q') {
     87             int x,y;
     88             scanf("%d%d",&x,&y);
     89             int l=1,r=len,ans=0;
     90             while(l<=r) {
     91                 int mid=(l+r)>>1;
     92                 if(check(mid,x+1,y+1)) l=mid+1,ans=mid;
     93                 else r=mid-1;
     94             }
     95             printf("%d
    ",ans);
     96         }
     97         else if(h[0]=='I') {
     98             int x;char y[10];
     99             scanf("%d%s",&x,y);
    100             insert(x+1,y[0]-'a'+1);
    101             len++;
    102         }
    103         else if(h[0]=='R') {
    104             int x;char y[10];
    105             scanf("%d%s",&x,y);
    106             change(rt,x+1,y[0]-'a'+1);
    107         }
    108     }
    109 }
    View Code
    O(∩_∩)O~ (*^__^*) 嘻嘻…… O(∩_∩)O哈哈~
  • 相关阅读:
    BZOJ3781 小B的询问
    BZOJ3757 苹果树
    BZOJ1491 [NOI2007]社交网络
    BZOJ3754 Tree之最小方差树
    BZOJ1251 序列终结者
    BZOJ2259 [Oibh]新型计算机
    BZOJ1043 [HAOI2008]下落的圆盘
    D. 预定义变量
    A. 变量命名原则
    B. PHP变量的特点
  • 原文地址:https://www.cnblogs.com/wls001/p/7678001.html
Copyright © 2011-2022 走看看