bzoj 1014: [JSOI2008]火星人prefix hash && splay

1014: [JSOI2008]火星人prefix

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 3154  Solved: 948
[Submit][Status]

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、 询问。语法:Q x y,x, y均为正整数。功能:计算LCQ(x, y) 限制:1 <= x, y <= 当前字符串长度。 2、 修改。语法:R x d,x是正整数,d是字符。功能:将字符串中第x个数修改为字符d。限制:x不超过当前字符串长度。 3、 插入:语法:I x d,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

数据规模:

对于100%的数据,满足:

1、 所有字符串自始至终都只有小写字母构成。

2、 M <= 150,000

3、 字符串长度L自始至终都满足L <= 100,000

4、 询问操作的个数不超过10,000个。

对于第1,2个数据,字符串长度自始至终都不超过1,000

对于第3,4,5个数据,没有插入操作。

  这道题在知道用hash做之后,还是不会,其实这道题用hash转换为另一道题,给定一个序列,支持插入,改变值,询问区间hash值,这样就可以用splay维护。

  一个需要注意的东西,字符串hash+自然溢出时,如果进制用的是偶数(26)是会挂掉滴。

  splay写的还是太慢了。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define MAXT 400000
#define MAXN 210000
unsigned int mi[MAXN];
struct Splay_tree
{
        int ch[MAXT][2],pnt[MAXT],val[MAXT],siz[MAXT];
        unsigned int hs[MAXT];
        int root;
        int topt;
        void update(int now)
        {
                siz[now]=siz[ch[now][0]]+siz[ch[now][1]]+1;
                hs[now]=hs[ch[now][0]] + val[now]*mi[siz[ch[now][0]]] + hs[ch[now][1]]*(mi[siz[ch[now][0]]+1]);
        }
        void rotate(int now)
        {
                int p=pnt[now],anc=pnt[p];
                int dir=(ch[p][0]==now);
                if (anc)ch[anc][ch[anc][1]==p]=now;
                pnt[now]=anc;
                pnt[ch[now][dir]]=p;
                ch[p][1-dir]=ch[now][dir];
                pnt[p]=now;
                ch[now][dir]=p;
                update(p);
                update(now);
        }
        void splay(int now,int tp=0)
        {
                while (now!=tp && pnt[now]!=tp)
                {
                        int p=pnt[now],anc=pnt[p];
                        if (anc==tp)
                        {
                                rotate(now);
                        }else
                        {
                                if((ch[anc][0]==p) == (ch[p][0]==now))
                                        rotate(p),rotate(now);
                                else
                                        rotate(now),rotate(now);
                        }
                }
                if (!tp)
                        root=now;
                return ;
        }
        int get_kth(int now,int rk)
        {
                if (!now)return -1;
                if (siz[ch[now][0]]+1==rk)
                        return now;
                else if (siz[ch[now][0]]+1<rk)
                        return get_kth(ch[now][1],rk-siz[ch[now][0]]-1);
                else return get_kth(ch[now][0],rk);
        }
        void insert(int pos,int v)
        {
                if (!root)
                {
                        root=++topt;
                        val[topt]=v;
                        pnt[topt]=0;
                        update(topt);
                        return ;
                }
                if (!pos)
                {
                        splay(get_kth(root,1),0);
                        ch[root][0]=++topt;
                        ch[topt][0]=ch[topt][1]=0;
                        val[topt]=v;
                        pnt[topt]=root;
                        update(topt);
                        update(root);
                        return ;
                }
                splay(get_kth(root,pos));
                if (pos+1<=siz[root])
                        splay(get_kth(root,pos+1),root);
                //scan(root);
                if (!ch[root][1])
                {
                        ch[root][1]=++topt;
                        pnt[topt]=root;
                }else
                {
                        ch[ch[root][1]][0]=++topt;
                        pnt[topt]=ch[root][1];
                }
                ch[topt][0]=ch[topt][1]=0;
                val[topt]=v;
                update(topt);
                if (ch[root][1]!=topt)
                        update(ch[root][1]);
                update(root);
        }
        void change_val(int pos,int v)
        {
                splay(get_kth(root,pos));
                val[root]=v;
                update(root);
        }
        unsigned int query(int l,int r)
        {
                if (l==1 && r==siz[root])
                {
    //                    scan(root);
                        return hs[root];
                }else if (l==1)
                {
                        splay(get_kth(root,r+1));
    //                    scan(ch[root][0]);printf("
");
                        return hs[ch[root][0]];
                }else if (r==siz[root])
                {
                        splay(get_kth(root,l-1));
    //                    scan(ch[root][1]);printf("
");
                        return hs[ch[root][1]];
                }else
                {
                        splay(get_kth(root,r+1));
                        splay(get_kth(root,l-1),root);
    //                    scan(ch[ch[root][0]][1]);printf("
");
                        return hs[ch[ch[root][0]][1]];
                }
        }
        void scan(int now)
        {
                if (!now)
                        return ;
                if (ch[now][0] && pnt[ch[now][0]]!=now)throw 1;
                if (ch[now][1] && pnt[ch[now][1]]!=now)throw 1;
                if (siz[now]!=siz[ch[now][0]]+siz[ch[now][1]]+1)throw 1;
                if (hs[now]!=hs[ch[now][0]]+val[now]*mi[siz[ch[now][0]]]+hs[ch[now][1]]*(mi[siz[ch[now][0]]+1]))throw 1;
                scan(ch[now][0]);
                printf("%d[%c] ",val[now],val[now]+'a');
                scan(ch[now][1]);
        }
}pp;
char str[MAXN];
int main()
{
        freopen("input.txt","r",stdin);
        mi[0]=1;
        int i;
        for (i=1;i<MAXN;i++)
                mi[i]=mi[i-1]*29;
        scanf("%s",str);
        int n=strlen(str);
        while (!islower(str[n-1]))
                n--;
        for (i=0;i<n;i++)
        {
                pp.insert(i,str[i]-'a');
        }
        int m;
        scanf("%d
",&m);
        char opt;
        char ch;
        int x,y;
        for (i=0;i<m;i++)
        {
                scanf("%c",&opt);
                if (opt=='Q')
                {
                        scanf("%d%d
",&x,&y);
                        if (x>y)swap(x,y);
                        int l=0,r=n-y+2,mid;
                        while(l+1<r)
                        {
                                mid=(l+r)>>1;
                                if (pp.query(x,x+mid-1)==pp.query(y,y+mid-1))
                                        l=mid;
                                else 
                                        r=mid;
                        }
                        printf("%d
",l);
                }else if (opt=='R')
                {
                        scanf("%d %c
",&x,&ch);
                        pp.change_val(x,ch-'a');
                }else if (opt=='I')
                {
                        scanf("%d %c
",&x,&ch);
                        pp.insert(x,ch-'a');
                        n++;
                }
        }
}
by mhy12345(http://www.cnblogs.com/mhy12345/) 未经允许请勿转载

本博客已停用,新博客地址:http://mhy12345.xyz

原文地址:https://www.cnblogs.com/mhy12345/p/4103318.html