bzoj1014 火星人prefix 字符串hash + 区间splay树

来源:互联网 发布:vip影视盒子源码 编辑:程序博客网 时间:2024/04/30 19:16

1014: [JSOI2008]火星人prefix

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 7256  Solved: 2326
[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树中的每个节点,维护两个信息,w[x]表示节点x对应当前序列中的s[i]的字符是多少,hsh[x]表示节点x子树对应序列s[l , l + 1 , ... , r]这个字符串的hash值为多少,根据一般字符串hash值的定义,我们可以得到update的更新方法:hsh[x] = w[x] * P[size[rc(x)]] + hsh[lc(x)] * P[size[rc(x)] + 1] + hsh[rc(x)]  , 其中lc(x)表示ch[x][0],rc(x)表示ch[x][1]


#pragma warning(disable:4786)#pragma comment(linker, "/STACK:102400000,102400000")#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<stack>#include<queue>#include<map>#include<set>#include<vector>#include<cmath>#include<string>#include<sstream>#include<bitset>#define LL long long#define ULL unsigned long long#define FOR(i,f_start,f_end) for(int i=f_start;i<=f_end;++i)#define mem(a,x) memset(a,x,sizeof(a))#define lson l,m,x<<1#define rson m+1,r,x<<1|1#define lc(x) ch[(x)][0]#define rc(x) ch[(x)][1]using namespace std;const int INF = 0x3f3f3f3f;const int mod = 1e9 + 7;const double PI = acos(-1.0);const double eps=1e-6;const ULL p = 131;const int maxn = 1e5 + 1000;int fa[maxn] , ch[maxn][2] ,  w[maxn] , size[maxn] ;ULL P[maxn] , hsh[maxn] , a[maxn];int st[maxn];     //st中存放回收的节点char s[maxn];int n , m , tot , root , top;       //root为伸展树的根,top为st的栈顶指针int newnode()       //拿出一个新的节点{    int   num = ++tot;    ch[num][0] = ch[num][1] = fa[num] = 0;    size[num] = 1;    return num;}void update(int x)      //相当于线段树的pushup功能{    if(!x)      return ;    size[x] = size[ch[x][0]] + size[ch[x][1]] + 1;    hsh[x] = (ULL)w[x] * P[size[rc(x)]] + hsh[lc(x)] * P[size[rc(x)] + 1] + hsh[rc(x)];}int dir(int x)      //若节点x是其父亲节点的左子节点,返回0;是右子节点,返回1{    return x == ch[fa[x]][1];}void rotate(int x){    int y , z , a , b , c;    y = fa[x];      z = fa[y];      b = dir(x);     a = ch[x][!b];    if(z == 0)        root = x;    else{        c = dir(y); ch[z][c] = x;    }    fa[x] = z;  fa[y] = x;  ch[x][!b] = y;  ch[y][b] = a;    if(a)       fa[a] = y;    update(y);      update(x);      //因为节点x和节点y为根子树中的节点改变了,所以要update}void splay(int x , int i)      //将x翻成节点i的直接子节点{    int y , z , b , c;    while(fa[x] != i){        y = fa[x];      z = fa[y];        if(z == i)      rotate(x);        else{            b = dir(x);     c = dir(y);            if(b ^ c){                                  //“之”字型                rotate(x);  rotate(x);            }            else{                                       //“一”字型                rotate(y);  rotate(x);            }        }    }}int find_k(int x , int k)         //在节点x为根的树对应的序列中,找第k个元素对应的值{    if(size[ch[x][0]] == k - 1)     return x;    if(size[ch[x][0]] > k - 1)      return find_k(ch[x][0] , k);    else    return find_k(ch[x][1] , k - size[ch[x][0]] - 1);}void build_tree(int l , int r , int tt)     //把一个序列a[l , l + 1 , ... , r]建成以节点tt为根的树{    int mid = l + (r - l) / 2;    w[tt] = a[mid];    if(l == r){        hsh[tt] = w[tt]  ;    size[tt] = 1;        return;    }    if(l < mid){        ch[tt][0] = newnode();      fa[ch[tt][0]] = tt;     build_tree(l , mid - 1 , ch[tt][0]);    }    if(mid < r){        ch[tt][1] = newnode();      fa[ch[tt][1]] = tt;     build_tree(mid + 1 , r , ch[tt][1]);    }    update(tt);}ULL Query(int l , int num)      //查询题目当前数列[l , l + 1 , ... , l + num - 1]的和{    int x = find_k(root , l);       splay(x , 0);      //为什么在root里找第l大数而不是l-1大?别忘了一开始插入了-INF节点    int y = find_k(root , l + num + 1);     splay(y , x);    return hsh[ch[y][0]];}void Insert(int l , char c){    int x = find_k(root , l + 1);       splay(x , 0);    int y = find_k(root , l + 2);         splay(y , x);    ch[y][0] = newnode();   fa[ch[y][0]] = y;    w[ch[y][0]] = c;    update(ch[y][0]);    update(y);      update(x);}void Replace(int l  , char c){    int x = find_k(root , l + 1);       splay(x , 0);    w[x] = c;    update(x);}void set_root(){    top = 0;    size[0] = w[0] = hsh[0] = 0;    tot = 2;    root = 1;    fa[1]= 0;   size[1] = 2;  ch[1][1] = 2; w[1] = hsh[1]= 0;     //插入两个无实际意义的节点,以防止使用上面函数时越界    fa[2] = 1;  size[2] = 1;  w[2] = hsh[2] = 0;}char op[20];int bsearch(int x, int y ,int left , int right){    int ret = 0 , mid;    while(left <= right){        mid = left + (right - left) / 2;        ULL temp1 = Query(x , mid) , temp2 = Query(y , mid);        if(temp1 == temp2){            ret = mid;            left = mid + 1;        }        else{            right = mid - 1;        }    }    return ret;}int main(){    P[0] = 1;    for(ULL i = 1 ; i <= 100002 ; i++){        P[i] = P[i - 1] * p;    }    scanf("%s" , s);    n = strlen(s);    for(int i =1 ; i <= n ; i++){        a[i] = s[i - 1];    }    set_root();    ch[2][0] = newnode();       fa[ch[2][0]] = 2;       //把初始序列插在两个占坑用的节点中间很关键    build_tree(1 , n , ch[2][0]);    update(2);      update(1);    int x , y;    scanf("%d" , &m);    int sz = n;    for(int _= 0 ; _ < m ; ++_){        scanf("%s" , op);        if(op[0] == 'Q'){            scanf("%d %d" , &x , &y);            int res = bsearch(x , y , 1 , min(sz - x + 1 , sz - y + 1));            printf("%d\n" , res);        }        else if(op[0] == 'R'){            scanf("%d %s" , &x , op);            Replace(x , op[0]);        }        else  if(op[0] == 'I'){            ++sz;            scanf("%d %s" , &x , op);            Insert(x , op[0]);        }    }    return 0;}


0 0
原创粉丝点击