bzoj1014: [JSOI2008]火星人prefix

3 篇文章 0 订阅

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判断

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#define LL long long
using namespace std;
const int Maxn = 100010;
const int Mod = 10000013;
int hash[Maxn], size[Maxn], cf[Maxn], val[Maxn], c[Maxn][2], tot, rt, fa[Maxn];
char s[Maxn]; int n, a[Maxn], m;
int _max ( int x, int y ){ return x > y ? x : y; }
void update ( int now ){
    int lc = c[now][0], rc = c[now][1];
    size[now] = size[lc]+size[rc]+1;
    hash[now] = (((LL)cf[size[rc]+1]*hash[lc])%Mod+((LL)cf[size[rc]]*val[now])%Mod+hash[rc])%Mod;
}
void bulid ( int &now, int l, int r, int f ){
    if ( l > r ) return;
    int mid = ( l + r ) >> 1;
    now = ++tot;
    val[now] = a[mid]; fa[now] = f;
    if ( l < r ){
        bulid ( c[now][0], l, mid-1, now );
        bulid ( c[now][1], mid+1, r, now );
    }
    update (now);
}
void rotate ( int x, int &k ){
    int y = fa[x], z = fa[y], l, r;
    if ( c[y][0] == x ) l = 0; else l = 1; r = l^1;
    if ( y == k ) k = x;
    else { if ( c[z][0] == y ) c[z][0] = x; else c[z][1] = x; }
    fa[x] = z; fa[y] = x; fa[c[x][r]] = y;
    c[y][l] = c[x][r]; c[x][r] = y;
    update (y);
}
void splay ( int x, int &k ){
    while ( x != k ){
        int y = fa[x], z = fa[y];
        if ( y != k ){
            if ( (c[y][0]==x)^(c[z][0]==y) ) rotate ( x, k );
            else rotate ( y, k );
        }
        rotate ( x, k );
    }
    update (x);
}
int get_rank ( int now, int rank ){
    int lc = c[now][0], rc = c[now][1];
    if ( size[lc]+1 == rank ) return now;
    else if ( size[lc] >= rank ) return get_rank ( lc, rank );
    else return get_rank ( rc, rank-size[lc]-1 );
}
void change ( int x, int k ){
    int p = get_rank ( rt, x+1 );
    splay ( p, rt );
    val[p] = k;
    update (p);
}
void insert ( int x, int k ){
    int p1 = get_rank ( rt, x+1 ), p2 = get_rank ( rt, x+2 );
    splay ( p1, rt ); splay ( p2, c[p1][1] );
    int p = ++tot;
    fa[p] = p2; val[p] = k; c[p2][0] = p;
    update (p); update (p2); update (p1);
}
int get_hash ( int x, int l ){
    int p1 = get_rank ( rt, x ), p2 = get_rank ( rt, x+l+1 );
    splay ( p1, rt ); splay ( p2, c[p1][1] );
    return hash[c[p2][0]];
}
int half_cut ( int x, int y ){
    int l = 1, r = n-_max ( x, y )+1, ret = 0;
    while ( l <= r ){
        int mid = ( l + r ) >> 1;
        if ( get_hash ( x, mid ) == get_hash ( y, mid ) ){ ret = mid; l = mid+1; }
        else r = mid-1;
    }
    return ret;
}
int main (){
    int i, j, k;
    cf[0] = 1;
    for ( i = 1; i <= 100005; i ++ ) cf[i] = (cf[i-1]*26)%Mod;
    scanf ( "%s", s+1 );
    n = strlen (s+1);
    for ( i = 1; i <= n; i ++ ) a[i] = s[i]-'a';
    bulid ( rt, 0, n+1, 0 );
    char ch;
    scanf ( "%d", &m );
    for ( i = 1; i <= m; i ++ ){
        getchar ();
        scanf ( "%c", &ch );
        if ( ch == 'Q' ){
            int x, y;
            scanf ( "%d%d", &x, &y );
            printf ( "%d\n", half_cut ( x, y ) );
        }
        else if ( ch== 'R' ){
            int x, y;
            scanf ( "%d", &x );
            getchar ();
            scanf ( "%c", &ch );
            y = ch-'a';
            change ( x, y );
        }
        else {
            int x, y;
            scanf ( "%d", &x );
            getchar ();
            scanf ( "%c", &ch );
            y = ch-'a';
            insert ( x, y );
            n ++;
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值