BZOJ1014: [JSOI2008]火星人prefix
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
7
Q 1 7
Q 4 8
Q 10 11
R 3 a
Q 1 7
I 10 a
Q 2 11
Sample Output
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個數據,沒有插入操作。
題解Here!
這個題,難到掉牙了。。。
看到字符串處理,坑能會想到?AC自動機/?SA?/?SAM 等等。
但是看到有插入與修改操作,?Splay?啊!
然后維護一個?hash?值:
a[rt].hash=a[lson].hash*val[a[rson].s+1]+val[a[rson].s]*(a[rt].v-'a'+1)+a[rson].hash;
//lson是rt的左孩子,rson是rt的右孩子
//a[rt].v表示rt節點存的字母
//a[rt].s表示rt節點的子樹大小
//val[i]表示第i位的hash值,即27^i
剩下的就是?Splay?基本操作,具體可見BZOJ1500: [NOI2005]維修數列
附代碼:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#define MAXN 100010
using namespace std;
int n,m;
int root=0,size=1;
char ch[MAXN];
unsigned val[MAXN];
struct Splay{int f,s,son[2];char v;unsigned hash;
}a[MAXN];
inline int read(){int date=0,w=1;char c=0;while(c<'0'||c>'9'){if(c=='-')w=-1;c=getchar();}while(c>='0'&&c<='9'){date=date*10+c-'0';c=getchar();}return date*w;
}
inline void pushup(int rt){if(!rt)return;int lson=a[rt].son[0],rson=a[rt].son[1];a[rt].s=a[lson].s+a[rson].s+1;a[rt].hash=a[lson].hash*val[a[rson].s+1]+val[a[rson].s]*(a[rt].v-'a'+1)+a[rson].hash;
}
inline void turn(int rt,int k){int x=a[rt].f,y=a[x].f;a[x].son[k^1]=a[rt].son[k];if(a[rt].son[k])a[a[rt].son[k]].f=x;a[rt].f=y;if(y)a[y].son[a[y].son[1]==x]=rt;a[x].f=rt;a[rt].son[k]=x;pushup(x);pushup(rt);
}
void splay(int rt,int ancestry){while(a[rt].f!=ancestry){int x=a[rt].f,y=a[x].f;if(y==ancestry)turn(rt,a[x].son[0]==rt);else{int k=a[y].son[0]==x?1:0;if(a[x].son[k]==rt){turn(rt,k^1);turn(rt,k);}else{turn(x,k);turn(rt,k);}}}if(ancestry==0)root=rt;
}
inline int newnode(char x){int rt=size++;a[rt].v=x;a[rt].hash=x-'a'+1;a[rt].s=1;a[rt].son[0]=a[rt].son[1]=0;return rt;
}
int buildtree(int l,int r){if(l>r)return 0;int mid=l+r>>1,lson=0,rson=0;lson=buildtree(l,mid-1);int rt=newnode(ch[mid]);rson=buildtree(mid+1,r);a[rt].son[0]=lson;a[rt].son[1]=rson;if(lson)a[lson].f=rt;if(rson)a[rson].f=rt;pushup(rt);return rt;
}
int kth(int rt,int k){while(1){int y=a[rt].son[0];if(k>a[y].s+1){k-=a[y].s+1;rt=a[rt].son[1];}else if(k<=a[y].s)rt=y;else return rt;}
}
inline int split(int l,int r){int front=kth(root,l),next=kth(root,r);splay(front,0);splay(next,front);return a[next].son[0];
}
inline void change(int x,char y){int front=kth(root,x),next=kth(root,x+2);splay(front,0);splay(next,front);int u=a[next].son[0];a[u].v=y;a[u].hash=y-'a'+1;pushup(next);pushup(front);
}
inline void insert(int x,char y){int front=kth(root,x),next=kth(root,x+1);splay(front,0);splay(next,front);int rt=newnode(y);a[next].son[0]=rt;a[rt].f=next;pushup(next);pushup(front);
}
inline bool check(int x,int y,int len){int u=split(x,x+len+1),hu=a[u].hash;int v=split(y,y+len+1),hv=a[v].hash;return (hu==hv);
}
void work(){char f[2],y[2];int x,k;while(m--){scanf("%s",f);x=read();if(f[0]=='Q'){k=read();int l=1,r=min(size-x,size-k)-2,mid;while(l<=r){mid=l+r>>1;if(check(x,k,mid))l=mid+1;else r=mid-1;}printf("%d\n",r);}if(f[0]=='R'){scanf("%s",y);change(x,y[0]);}if(f[0]=='I'){scanf("%s",y);insert(x+1,y[0]);}}
}
void init(){scanf("%s",ch+1);n=strlen(ch+1);val[0]=1;for(int i=1;i<=MAXN-5;i++)val[i]=val[i-1]*27;root=buildtree(0,n+1);m=read();
}
int main(){init();work();return 0;
}
?