【bzoj 1014】 火星人prefix 【JSOI2008】

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+二分解决,首先用splay维护这个序列,每个节点存储以该节点为根节点的子树的hash值,然后对于每个询问,二分可能的最大长度,并用hash判断是否可行,下面是程序:

#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#define ll unsigned long long
using namespace std;
const int N=100005;
struct tree{
	int ch[2],f,cnt;
	ll w,c;
}a[N];
ll p[N]={1};
int k,root,len;
char tp[N];
void updata(int x){
	a[x].cnt=a[a[x].ch[0]].cnt+a[a[x].ch[1]].cnt+1;
	a[x].w=a[a[x].ch[0]].w*p[a[a[x].ch[1]].cnt+1]
	      +a[x].c*p[a[a[x].ch[1]].cnt]+a[a[x].ch[1]].w;
}
int build(int f,int l,int r){
	int rt=++k;
	if(l==r){
		a[rt].f=f;
		a[rt].c=a[rt].w=tp[l]-'a'+1;
		a[rt].ch[0]=a[rt].ch[a[rt].cnt=1]=0;
		return rt;
	}
	if(l+1==r){
		a[rt].f=f;
		a[rt].cnt=2;
		a[rt].c=tp[l]-'a'+1;
		a[rt].ch[1]=build(rt,r,r);
		updata(rt);
		return rt;
	}
	int m=l+r>>1;
	a[rt].f=f;
	a[rt].cnt=r-l+1;
	a[rt].c=tp[m]-'a'+1;
	a[rt].ch[0]=build(rt,l,m-1);
	a[rt].ch[1]=build(rt,m+1,r);
	updata(rt);
	return rt;
}
void into(){
	int i;
	for(i=1;i<N;i++){
		p[i]=p[i-1]*101;
	}
	char c=getchar();
	while(c<'a'||c>'z'){
		c=getchar();
	}
	while(c>='a'&&c<='z'){
		tp[len++]=c;
		c=getchar();
	}
	root=build(0,0,len-1);
}
int find(int x){
	int r=root;
	while(a[a[r].ch[0]].cnt+1!=x){
		if(a[a[r].ch[0]].cnt+1>x){
			r=a[r].ch[0];
		}
		else{
			x-=a[a[r].ch[0]].cnt+1;
			r=a[r].ch[1];
		}
	}
	return r;
}
void rotate(int x,bool kind){
	int y=a[x].f;
	int z=a[y].f;
	a[y].ch[!kind]=a[x].ch[kind];
	a[a[x].ch[kind]].f=y;
	if(z){
		a[z].ch[a[z].ch[1]==y]=x;
	}
	a[x].f=z;
	a[x].ch[kind]=y;
	a[y].f=x;
	updata(y);
	updata(x);
}
void splay(int x,int t){
	while(a[x].f!=t){
		int y=a[x].f;
		int z=a[y].f;
		if(z!=t){
			bool kind=a[z].ch[0]==y;
			if(a[y].ch[kind]==x){
				rotate(x,!kind);
				rotate(x,kind);
			}
			else{
				rotate(y,kind);
				rotate(x,kind);
			}
		}
		else{
			rotate(x,x==a[y].ch[0]);
		}
	}
	updata(x);
	if(!t){
		root=x;
	}
}
void insert(int x,ll w){
	k++;
	len++;
	a[k].cnt=1;
	a[k].w=a[k].c=w;
	if(x==0){
		splay(find(1),0);
		a[k].ch[0]=a[k].f=0;
		a[k].ch[1]=root;
		a[root].f=k;
		root=k;
		updata(root);
		return;
	}
	if(x==len-1){
		splay(find(len-1),0);
		a[k].ch[1]=a[k].f=0;
		a[k].ch[0]=root;
		a[root].f=k;
		root=k;
		updata(root);
		return;
	}
	int q=find(x);
	splay(find(x),0);
	splay(find(x+1),0);
	a[k].ch[0]=a[k].ch[1]=0;
	a[k].f=a[root].ch[0];
	a[a[root].ch[0]].ch[1]=k;
	updata(a[root].ch[0]);
	updata(root);
}
char ch(){
	char c=getchar();
	while(c!='Q'&&c!='R'&&c!='I'){
		c=getchar();
	}
	return c;
}
void read(char &c){
	c=getchar();
	while(c<'a'||c>'z'){
		c=getchar();
	}
}
ll h(int x,int t){
	if(x==1){
		splay(find(x+t),0);
		return a[a[root].ch[0]].w;
	}
	if(x+t==len+1){
		splay(find(x-1),0);
		return a[a[root].ch[1]].w;
	}
	int y=find(x-1);
	splay(y,0);
	splay(find(x+t),y);
	return a[a[a[root].ch[1]].ch[0]].w;
}
int LCQ(int x,int y){
	if(x==y){
		return len-x+1;
	}
	int l=0,r=len-max(x,y)+1;
	while(l<r){
		int mid=l+r+1>>1;
		if(h(x,mid)==h(y,mid)){
			l=mid;
		}
		else{
			r=mid-1;
		}
	}
	return l;
}
void change(int x,ll w){
	splay(find(x),0);
	a[root].c=w;
	updata(root);
}
int main(){
	into();
	int m,x,y;
	char c;
	scanf("%d",&m);
	while(m--){
		c=ch();
		if(c=='Q'){
			scanf("%d%d",&x,&y);
			printf("%d\n",LCQ(x,y));
			continue;
		}
		if(c=='R'){
			scanf("%d",&x);
			read(c);
			change(x,c-'a'+1);
			continue;
		}
		if(c=='I'){
			scanf("%d",&x);
			read(c);
			insert(x,c-'a'+1);
			continue;
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/tlecoce/article/details/79827071
今日推荐