您的位置:首页 > Web前端 > JavaScript

BZOJ 1014 [JSOI2008]火星人prefix

2016-06-17 12:02 375 查看
title: ‘BZOJ 1014 [JSOI2008]火星人prefix’

categories: BZOJ

date: 2015-12-28 19:49:00

tags: [Splay,Hash,二分]

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.txt

madamimadam

7

Q 1 7

Q 4 8

Q 10 11

R 3 a

Q 1 7

I 10 a

Q 2 11

output.txt

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个数据,没有插入操作。

Solution

对字符串的进行哈希,然后二分LCP长度,即可求出LCP(x,y)。运用同样的方法也可以做这道题,splay维护字符串序列,同时update时维护子树的哈希值即可,具体操作见代码。

Code

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>

#define maxn 100000+5

using namespace std;

typedef unsigned long long ll;

struct Splay_Tree{
int ch[2];
int sz;
ll haha; char key;
}tr[maxn<<1];

const ll ha=14233333;

char s[maxn];
ll x[maxn];
int n,m,root,cnt,len;

inline void Update(int k){
tr[k].sz=tr[tr[k].ch[0]].sz+tr[tr[k].ch[1]].sz+1;
tr[k].haha=tr[tr[k].ch[0]].haha+x[tr[tr[k].ch[0]].sz]*tr[k].key+tr[tr[k].ch[1]].haha*x[tr[tr[k].ch[0]].sz+1];
}

void Build(int l,int r,int &k){
if(l>r) return;
if(l==r){
k=++cnt;
tr[k].ch[0]=tr[k].ch[1]=0;
tr[k].sz=1; tr[k].key=s[l]; tr[k].haha=s[l];
return;
}
int mid=(l+r)>>1; k=++cnt;
Build(l,mid-1,tr[k].ch[0]);
tr[k].key=s[mid];
Build(mid+1,r,tr[k].ch[1]);
Update(k);
}

void Rotate(int &k,int d){
int t=tr[k].ch[d^1];
tr[k].ch[d^1]=tr[t].ch[d]; tr[t].ch[d]=k;
Update(k); Update(t); k=t;
}

void Splay(int &k,int x){
int d1=(tr[tr[k].ch[0]].sz<x?1:0),t=tr[k].ch[d1];
if(d1==1) x-=tr[tr[k].ch[0]].sz+1;
if(x){
int d2=(tr[tr[t].ch[0]].sz<x?1:0);
if(d2==1) x-=tr[tr[t].ch[0]].sz+1;
if(x){
Splay(tr[t].ch[d2],x);
if(d1==d2) Rotate(k,d1^1);
else Rotate(tr[k].ch[d1],d1);
}
Rotate(k,d1^1);
}
}

void Insert(int x,char val){
Splay(root,x+1); Splay(tr[root].ch[1],x+1-tr[tr[root].ch[0]].sz);
int k=++cnt;
tr[k].key=val; tr[k].ch[0]=tr[k].ch[1]=0;
tr[tr[root].ch[1]].ch[0]=k;
Update(k); Update(tr[root].ch[1]); Update(root);
}

void Modify(int x,char val){
Splay(root,x+1);
tr[root].key=val;
Update(root);
}

bool check(int num,int x,int y){
ll haha1,haha2;
Splay(root,x); Splay(tr[root].ch[1],x+num-tr[tr[root].ch[0]].sz);
haha1=tr[tr[tr[root].ch[1]].ch[0]].haha;
Splay(root,y); Splay(tr[root].ch[1],y+num-tr[tr[root].ch[0]].sz);
haha2=tr[tr[tr[root].ch[1]].ch[0]].haha;
if(haha1==haha2) return true;
return false;
}

int Query(int x,int y){
int l=0,r=len-y+1;
while(l<r){
int mid=(l+r+1)>>1;
if(check(mid,x,y)) l=mid;
else r=mid-1;
}
return l;
}

void init(){
x[0]=1;
for(int i=1;i<maxn;i++) x[i]=x[i-1]*27;
scanf("%s",s+1); root=0;
len=strlen(s+1);
Build(0,len+2,root)
}

int main(){
freopen("1014.in","r",stdin);
init();
scanf("%d",&n);
for(int i=1;i<=n;i++){
char ctrl[1];
scanf("%s",ctrl);
if(ctrl[0]=='Q'){
int x,y;
scanf("%d%d",&x,&y);
if(x>y) swap(x,y);
printf("%d\n",Query(x,y));
}
else if(ctrl[0]=='I'){
int x; char y;
scanf("%d %c",&x,&y);
Insert(x,y); len++;
}
else{
int x; char y;
scanf("%d %c",&x,&y);
Modify(x,y);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  hash Splay 二分