2105: 增强型LCP
Description
Input
Output
对于每个Lcp(a,b)操作输出最长公共前缀
Sample Input
47
abab
L 13
A 1 ab
L 1 3
C 56 cb
L 1 3
D 1 2
L 1 3
abab
L 13
A 1 ab
L 1 3
C 56 cb
L 1 3
D 1 2
L 1 3
Sample Output
2
4
2
0
4
2
0
不会在splay上求lcp,如果没有这个操作就是裸的splay操作了。。。
然而加上了就不会了...
大家说因为修改数少,所以暴力重建然后二分hash求lcp即可。。。
1 #include<iostream> 2 #include<cstdlib> 3 #include<cmath> 4 #include<cstring> 5 #include<cstdio> 6 #include<algorithm> 7 #include<string> 8 #include<map> 9 #include<queue> 10 #include<vector> 11 #include<set> 12 #define inf 1000000000 13 #define maxn 1000000+5 14 #define maxm 10000+5 15 #define eps 1e-10 16 #define ll long long 17 #define ull unsigned long long 18 #define base 13131 19 #define for0(i,n) for(int i=0;i<=(n);i++) 20 #define for1(i,n) for(int i=1;i<=(n);i++) 21 #define for2(i,x,y) for(int i=(x);i<=(y);i++) 22 #define for3(i,x,y) for(int i=(x);i>=(y);i--) 23 #define for4(i,x) for(int i=head[x],y=e[i].go;i;i=e[i].next,y=e[i].go) 24 using namespace std; 25 int read(){ 26 int x=0,f=1;char ch=getchar(); 27 while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} 28 while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();} 29 return x*f; 30 } 31 int n,m,q; 32 ull a[maxn],b[maxn]; 33 char ch[maxn]; 34 string s; 35 void rebuild(){ 36 n=s.length(); 37 b[n-1]=s[n-1]; 38 for3(i,n-2,0)b[i]=b[i+1]*base+(ull)s[i]; 39 } 40 ull get(int x,int l){ 41 return b[x]-b[x+l]*a[l]; 42 } 43 int main(){ 44 //freopen("input.txt","r",stdin); 45 //freopen("output.txt","w",stdout); 46 n=read();q=read(); 47 scanf("%s",ch);s=ch; 48 a[0]=1; 49 for1(i,maxn-1)a[i]=a[i-1]*(ull)base; 50 rebuild(); 51 while(q--){ 52 scanf("%s",ch); 53 if(ch[0]=='L'){ 54 int x=read()-1,y=read()-1,l=0,r=n-y; 55 while(l<=r){ 56 int mid=(l+r)>>1; 57 if(get(x,mid)==get(y,mid))l=mid+1; 58 else r=mid-1; 59 } 60 printf("%d\n",r); 61 } 62 else if(ch[0]=='A'){ 63 int x=read()-1; 64 scanf("%s",ch); 65 s.insert(x,ch); 66 rebuild(); 67 } 68 else if(ch[0]=='C'){ 69 int x=read()-1,y=read()-1; 70 scanf("%s",ch); 71 for2(i,x,y) 72 s[i]=ch[i-x]; 73 rebuild(); 74 } 75 else{ 76 int x=read()-1,y=read()-1; 77 s.erase(x,y-x+1); 78 rebuild(); 79 } 80 } 81 return 0; 82 }