1014: [JSOI2008]火星人prefix

1014: [JSOI2008]火星人prefix

Time Limit: 10 Sec Memory Limit: 162 MB

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

题目分析

由于此题的字符串是动态的,所以显然不可以用后缀数组。

然而我sb的以为最开始这是一道后缀数组

我们用二分加hash来求LCQ,由于有插入和修改操作,所以我们要选择一个动态的数据结构。显然可以用splay(在网上我看见有人写可伸展的treap,Orz!)。

于是此题就变得比较简单了,hash+splay+二分,复杂度为((M log len log len))

#include <cstdio>
#include <cstring>
inline int geti() {
	register int a; register char c;
	while(c=getchar(),c<'-');a=c-'0';
	while(c=getchar(),'-'<c)a=(a<<3)+(a<<1)+c-'0';
	return a;
}
typedef unsigned int ui;
const int N=150000,ba=257;
ui p[N];
struct node {
	ui ha; int siz; char c;
	node *fa,*ch[2];
}CD[N],*cd=CD,*root=NULL,*nil;
inline void maintain(node *(&u)) {
	if(u==nil) return;
	u->siz=u->ch[0]->siz+u->ch[1]->siz+1;
	u->ha=u->ch[0]->ha+u->c*p[u->ch[0]->siz]+u->ch[1]->ha*p[u->ch[0]->siz+1];
}
inline void rot(node *(&u)) {
	node *f=u->fa,*ff=f->fa;
	int d=u==f->ch[1];
	if((f->ch[d]=u->ch[d^1])!=nil) f->ch[d]->fa=f;
	f->fa=u,u->ch[d^1]=f;
	if((u->fa=ff)!=nil) ff->ch[f==ff->ch[1]]=u;
	maintain(f),maintain(u);
}
inline void splay(node *(&u),node *(&tag)) {
	while(u->fa!=tag) {
		if(u->fa->fa==tag) {rot(u);break;}
		if((u==u->fa->ch[1])==(u->fa==u->fa->fa->ch[1])) rot(u->fa);
		else rot(u); rot(u);
	}if(tag==nil) root=u;
}
inline node *kth(int k) {
	node *u=root;
	while(k&&u!=nil) {
		if(u->ch[0]->siz>=k) u=u->ch[0];
		else if(u->ch[0]->siz+1==k) return u;
		else k-=u->ch[0]->siz+1,u=u->ch[1];
	}return u;
}
inline void ins(int k, char ch) {
	node *l=kth(k); cd->c=ch;
	cd->ch[0]=nil,cd->ch[1]=l->ch[1];
	cd->fa=l,cd->ch[1]->fa=cd;
	l->ch[1]=cd;
	maintain(cd);
	while(l!=nil) maintain(l),l=l->fa;
	++cd;//splay(cd++,nil);
}
inline void upd(int k,char ch) {
	node *l=kth(k),*u; l->c=ch;
	for(u=l;u!=nil;u=u->fa) maintain(u);
	//splay(l,nil);
}
inline ui quy(int l,int r) {
	node *lu=kth(l-1),*ru=kth(r+1);
	splay(lu,nil); splay(ru,lu);
	return ru->ch[0]->ha;
}
char initstring[N];
node *build(int l,int r) {
	if(l>r) return nil;
	int m=l+r>>1; node *u=cd++;
	u->c=initstring[m];
	if((u->ch[0]=build(l,m-1))!=nil) u->ch[0]->fa=u;
	if((u->ch[1]=build(m+1,r))!=nil) u->ch[1]->fa=u;
	maintain(u); return u;
}
void PRINT(node *u) {
	if(u==nil) return;
	PRINT(u->ch[0]);
	putchar(u->c);
	PRINT(u->ch[1]);
}
int main() {
#ifndef ONLINE_JUDGE
	freopen("1014.in","r",stdin);
//	freopen("1014.out","w",stdout);
#endif
	initstring[1]='~';
	scanf("%s",initstring+2);
	register int cl=strlen(initstring+2),i,j,mi,l,r,m,an;
	for(p[0]=i=1;i<N;++i) p[i]=p[i-1]*ba;
	initstring[cl+2]='!';
	cd->fa=cd->ch[0]=cd->ch[1]=cd;
	cd->ha=cd->siz=cd->c=0; nil=cd++;
	root=build(1,cl+2); root->fa=nil;
	register char ch;
	for(m=geti();m;--m) {
		while(ch=getchar(),ch<'A');
		if(ch=='Q') {
			i=geti()+1,j=geti()+1;
//			if(m==1) PRINT(root),puts("");
			if(kth(i)->c!=kth(j)->c) {puts("0"); continue;}
			for(an=l=1,r=cl-(i<j?j:i)+2;l<=r;) {
				mi=l+r>>1;
				if(quy(i,i+mi-1)==quy(j,j+mi-1)) l=(an=mi)+1;
				else r=mi-1; //PRINT(root); puts("");
			}printf("%d
",an);
		} else {
			j=ch=='R'; cl+=j^1;
			i=geti()+1; while(ch=getchar(),ch<'a');
			(j)?upd(i,ch):ins(i,ch);
		}
//		PRINT(root),puts("");
	}
}

原文地址:https://www.cnblogs.com/cycleke/p/6044813.html