zoukankan      html  css  js  c++  java
  • BZOJ 1014: [JSOI2008]火星人prefix [splay 二分+hash] 【未完】

    1014: [JSOI2008]火星人prefix

    Time Limit: 10 Sec  Memory Limit: 162 MB
    Submit: 6243  Solved: 2007
    [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的方法求LCQ

    还有问题,一直RE,以后再写一遍吧,求数据

    先说几个已经debug过的问题:

    1.二分的r的取值

    2.size问题,要手动=1

    3.hash会溢出int

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    typedef long long ll;
    #define lc t[x].ch[0]
    #define rc t[x].ch[1]
    #define pa t[x].fa
    const int N=1e5+5,INF=1e9,MOD=9875321;
    typedef long long ll;
    int read(){
        char c=getchar();int x=0,f=1;
        while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();}
        while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();}
        return x*f;
    }
    int n,m,x,y;char s[N],op[2],c[2];
    ll po[N];
    void init(){
        po[0]=1;
        for(int i=1;i<=100000;i++) po[i]=po[i-1]*27%MOD;
    }
    struct node{
        int ch[2],fa,v,size;
        ll hash;
    }t[N];
    int root,cnt;
    inline int wh(int x){return t[pa].ch[1]==x;}
    inline void update(int x){
        t[x].size=t[lc].size+t[rc].size+1;
        t[x].hash=((ll)t[lc].hash*po[t[rc].size+1]%MOD+(ll)t[x].v*po[t[rc].size]%MOD+t[rc].hash)%MOD;
    }
    inline void rotate(int x){
        int f=t[x].fa,g=t[f].fa,c=wh(x);
        if(g) t[g].ch[wh(f)]=x;t[x].fa=g;
        t[f].ch[c]=t[x].ch[c^1];t[t[f].ch[c]].fa=f;
        t[x].ch[c^1]=f;t[f].fa=x;
        update(f);update(x);
    }
    void splay(int x,int tar){
        for(;t[x].fa!=tar;rotate(x))
            if(t[pa].fa!=tar) rotate(wh(x)==wh(pa)?pa:x);
        if(tar==0) root=x;
    }
    int build(int l,int r){
        if(l>r) return 0;
        int x=(l+r)>>1;
        t[x].v=s[x-1]-'a';t[x].size=1;
        lc=build(l,x-1);rc=build(x+1,r);
        if(lc) t[lc].fa=x;
        if(rc) t[rc].fa=x;
        update(x);
        return x;
    }
    int kth(int k){
        int x=root,ls=0;
        while(x){
            int _=ls+t[lc].size;
            if(k==_+1) break;
            if(k<=_) x=lc;
            else ls=_+1,x=rc;
        }
        return x;
    }
    void change(int k,char c){
        int x=kth(k+1);splay(x,root);
        t[x].v=c-'a';
        update(root);
    }
    void insert(int k,char c){
        int y=kth(k+1),x=kth(k+2);
        splay(y,0);splay(x,root);
        lc=++cnt;
        t[lc].v=c-'a';t[lc].size=1;t[lc].fa=x;
        update(lc);update(x);update(y);
    }
    int hash(int k,int l){
        int y=kth(k),x=kth(k+l+1);
        splay(y,0);splay(x,root);
        return t[lc].hash;
    }
    int query(int x,int y){
        int l=1,r=min(t[root].size-x,t[root].size-y)-1,ans=0;
        while(l<=r){
            int mid=(l+r)>>1;
            if(hash(x,mid)==hash(y,mid)) l=mid+1,ans=mid;
            else r=mid-1;
        }
        return ans;
    }
    int main(){
        //freopen("t.in","r",stdin);
        //freopen("t.out","w",stdout);
        scanf("%s%d",s+1,&m);
        n=strlen(s+1);
        init();
        t[0].size=t[0].hash=0;
        root=build(1,n+2);cnt=n+2;
        while(m--){
            scanf("%s",op);x=read();
            if(op[0]=='Q') y=read(),printf("%d
    ",query(x,y));
            else if(op[0]=='R') scanf("%s",c),change(x,c[0]);
            else scanf("%s",c),insert(x,c[0]);
        }
    }
  • 相关阅读:
    dsoframer设计笔记
    pb 使用ole控制进行WORD操作失败-9
    【学习笔记】Fragment
    Suggest:the suffix for classes name
    ArrayList和LinkedList
    Android Studio入门
    asp.net ToString() 输出格式详细
    Uploadify 3.2 参数属性、事件、方法函数详解
    基础解析正则表达式
    10个优秀的 Web UI库/框架
  • 原文地址:https://www.cnblogs.com/candy99/p/6160462.html
Copyright © 2011-2022 走看看