• 1014: [JSOI2008]火星人prefix


    Time Limit: 10 Sec  Memory Limit: 162 MB
    Submit: 8968  Solved: 2865
    [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个数据,没有插入操作。

      1 #include<iostream>
      2 #include<cstdio>
      3 #include<cstring>
      4 using namespace std;
      5 
      6 #define LL long long
      7 const int MAXN=150000+5;
      8 const int mod=9875321;
      9 
     10 struct node
     11 {
     12     int left,right,father;
     13     int hash,val,size;
     14 }tree[MAXN];
     15 
     16 char ch[MAXN];
     17 int n,m,tot,rt;
     18 int p[MAXN];
     19 
     20 void update(int k)
     21 {
     22     int l=tree[k].left,r=tree[k].right;
     23     tree[k].size=tree[l].size+tree[r].size+1;
     24     tree[k].hash=tree[l].hash+(LL)tree[k].val*p[tree[l].size]%mod+tree[r].hash*(LL)p[tree[l].size+1]%mod;
     25     tree[k].hash%=mod;
     26 }
     27 
     28 void rotate(int x,int &k)
     29 {
     30     int y=tree[x].father,z=tree[y].father;
     31     if(y==k) k=x;
     32     else
     33     {
     34         if(tree[z].left==y)
     35             tree[z].left=x;
     36         else tree[z].right=x;
     37     }
     38     tree[x].father=z;tree[y].father=x;
     39     if(tree[y].left==x)
     40     {
     41         tree[tree[x].right].father=y;
     42         tree[y].left=tree[x].right;
     43         tree[x].right=y;
     44     }
     45     else
     46     {
     47         tree[tree[x].left].father=y;
     48         tree[y].right=tree[x].left;
     49         tree[x].left=y;
     50     }
     51     update(y);update(x);
     52 }
     53 
     54 void splay(int x,int &k)
     55 {
     56     while(x!=k)
     57     {
     58         int y=tree[x].father,z=tree[y].father;
     59         if(y!=k)
     60         {
     61             if((tree[y].left==x)^(tree[z].left==y))
     62                 rotate(x,k);
     63             else rotate(y,k);
     64         }
     65         rotate(x,k);
     66     }
     67 }
     68 
     69 int find(int k,int rk)
     70 {
     71     int l=tree[k].left,r=tree[k].right;
     72     if(tree[l].size+1==rk) return k;
     73     else if(tree[l].size>=rk) return find(l,rk);
     74     else return find(r,rk-tree[l].size-1);
     75 }
     76 
     77 void insert(int k,int val)
     78 {
     79     int x=find(rt,k+1),y=find(rt,k+2);
     80     splay(x,rt);splay(y,tree[x].right);
     81     int z=++tot;
     82     tree[y].left=z;tree[z].father=y;tree[z].val=val;
     83     update(z);update(y);update(x);
     84 }
     85 
     86 int query(int k,int val)
     87 {
     88     int x=find(rt,k),y=find(rt,k+val+1);
     89     splay(x,rt);splay(y,tree[x].right);
     90     int z=tree[y].left;
     91     return tree[z].hash;
     92 }
     93 
     94 int solve(int x,int y)
     95 {
     96     int l=1,r=min(tot-x,tot-y)-1,ans=0;
     97     while(l<=r)
     98     {
     99         int mid=(l+r)>>1;
    100         if(query(x,mid)==query(y,mid))
    101         {
    102             l=mid+1;
    103             ans=mid;
    104         }
    105         else r=mid-1;
    106     }
    107     return ans;
    108 }
    109 
    110 void build(int l,int r,int last)
    111 {
    112     if(l>r) return;
    113     if(l==r)
    114     {
    115         tree[l].val=tree[l].hash=ch[l]-'a';
    116         tree[l].father=last;tree[l].size=1;
    117         if(l<last) tree[last].left=l;
    118         else tree[last].right=l;
    119         return;
    120     }
    121     int mid=(l+r)>>1;
    122     build(l,mid-1,mid);build(mid+1,r,mid);
    123     tree[mid].val=ch[mid]-'a';tree[mid].father=last;
    124     update(mid);
    125     if(mid<last) tree[last].left=mid;
    126     else tree[last].right=mid;
    127 }
    128 
    129 int main()
    130 {
    131     scanf("%s",ch+2);
    132     scanf("%d",&m);
    133     n=strlen(ch+2);
    134     p[0]=1;
    135     for(int i=1;i<=MAXN;i++) p[i]=p[i-1]*27%mod;
    136     build(1,n+2,0);
    137     tot=n+2;rt=(n+3)/2;
    138     while(m--)
    139     {
    140         char S[2],d[2];
    141         int x,y;
    142         scanf("%s",S);
    143         scanf("%d",&x);
    144         switch(S[0])
    145         {
    146             case 'Q':
    147                 scanf("%d",&y);
    148                 printf("%d
    ",solve(x,y));
    149                 break;
    150             case 'R':
    151                 scanf("%s",d);
    152                 x=find(rt,x+1);
    153                 splay(x,rt);
    154                 tree[rt].val=d[0]-'a';
    155                 update(rt);
    156                 break;
    157             case 'I':
    158                 scanf("%s",d);
    159                 insert(x,d[0]-'a');
    160                 break;
    161         }
    162     }
    163     return 0;
    164 }
  • 相关阅读:
    加密API实现与测试
    使用GO操作MongoDB
    go mongodb Pipeline 聚合查询格式
    [ZJOI 2022] 众数
    Generators All in One
    [Contest on 2022.5.27] 我还是一道题都不会做
    [干饭现场] 多项式全家桶
    [题目小结] 线性姬
    [Contest on 2022.5.31] 在 5 月 31 日的考试
    CodeForces 917D Stranger Trees
  • 原文地址:https://www.cnblogs.com/InWILL/p/9296544.html
Copyright © 2020-2023  润新知