Description
The Martians have recently studied an operation that asks for a common prefix of two suffixes of a string. For example, there is a string like this: Madamimadam,
We label each character of this string: ordinal: 1 2 3 4 5 6 7 8 9 10 11 character M A d a m i m a D a m now,
The Martians define a function Lcq (x, y), which is a string of characters starting with the first x character of the substring, and a string starting with the nth character
The length of the public prefix of two strings. For example, LCQ (1, 7) = 5, LCQ (2, ten) = 1, LCQ (4, 7) = 0 in the process of studying the LCQ function
, the Martians discovered such an association: if all suffixes of the string are ordered, the value of the LCQ function can be obtained quickly;
If you find the value of the LCQ function, you can also quickly sequence the suffix of the string. Although the Martians were clever enough to find the fast LCQ function
Algorithm, but the people who are unwilling to concede to the earth have made a difficult problem for the Martians: they can also change the string itself while seeking the LCQ function. Specifically
, you can change the value of one of the characters in the string, or you can insert a character at one point in the string. Earth people want to test, in so
Complex question, whether the Martians can also be able to quickly find the value of the LCQ function.
Solution
See LongestComonQianzhui2333 manual funny
If you don't know it, it's recommended to read the classic training Guide to algorithmic competition 3.4.3 LCP algorithm based on hash value
Maintain it with splay.
T[X].HASH=T[LC (x)].hash+ (t[x].val-'a') *base[T[LC (x)].SIZ]+T[RC (x)]. hash*Base[T[LC (x)].siz+1]
#include <iostream>#include<cstdio>#include<cstring>#include<cstdlib>#defineMAXN 100005#defineLC (x) t[x].ch[0]#defineRC (x) t[x].ch[1]#defineMin (A, B) (A<B?A:B)using namespacestd;intM,root,siz;Chars[maxn];unsignedLong Long Base[MAXN];structnode{intch[2],father,siz; unsignedLong LongHash; CharVal; Node () {ch[0]=ch[1]=father=siz=hash=0;}} T[MAXN];intRead () {intx=0, f=1;CharC=GetChar (); while(c<'0'|| C>'9'){ if(c=='-') f=-1; c=GetChar (); } while(c>='0'&&c<='9') {x=x*Ten+c-'0'; c=GetChar (); } returnx*F;}voidUpdate (intx) {T[x].siz=T[LC (x)].SIZ+T[RC (x)].siz+1; T[x].hash= (t[x].val-'a')*Base[T[LC (x)].siz]; T[x].hash+=T[LC (x)].HASH+T[RC (x)].hash*Base[T[LC (x)].siz+1];}voidRotate (intXint&k) { inty=T[x].father; intz=T[y].father; intP= (t[y].ch[0]==x)?0:1; if(y==k) k=x; Else { if(t[z].ch[0]==y) t[z].ch[0]=x; Elset[z].ch[1]=x; } t[x].father=Z; T[Y].CH[P]=t[x].ch[p^1]; T[t[x].ch[p^1]].father=y; T[x].ch[p^1]=y; T[y].father=x; Update (y); Update (x);}voidSplay (intXint&k) { while(x!=k) {inty=T[x].father; intz=T[y].father; if(y!=k) {if((t[y].ch[0]==x) ^ (t[z].ch[0]==y)) Rotate (x,k); ElseRotate (y,k); } Rotate (X,k); }}intBuild (intLintRintf) { if(L>r)return 0; intnow= (l+r) >>1; T[now].father=F; T[now].val=S[now]; t[now].ch[0]=build (l,now-1, now); t[now].ch[1]=build (now+1, R,now); Update (now); returnNow ;}intFind (intXintk) { if(!x)return 0; if(T[LC (x)].siz>=k)returnFind (LC (x), k); if(T[LC (x)].siz+1<K)returnFind (RC (x), K-T[LC (x)].siz-1); returnx;}intCheckintXinty) { intA=find (root,x), B=find (root,y+2); Splay (A,root); Splay (B,RC (root)); returnT[LC (RC (root))].hash;}voidQuery (intXinty) { intL=1, R=min (siz-x,siz-y)-1, ans=0; while(l<=r) {intMid= (l+r) >>1; if(Check (x,x+mid-1) ==check (y,y+mid-1)) Ans=mid,l=mid+1; Elser=mid-1; } printf ("%d\n", ans);}voidChange (intXChard) { intA=find (root,x+1); Splay (A,root); T[a].val=D; Update (a);}voidInsert (intXChard) { intA=find (root,x+1), B=find (root,x+2); Splay (A,root); Splay (B,RC (root)); Siz++;T[RC (Root)].ch[0]=siz; T[siz].val=d,t[siz].father=RC (root); Update (siz); Update (RC (root)); Update (root);}intMain () {Base[0]=1; for(intI=1; i<maxn;i++)Base[i]=Base[I1]* -; scanf ("%s", s+2); Siz=strlen (s+2)+2; s[1]=s[siz]=0; M=Read (); Root=build (1, Siz,0); for(intI=1; i<=m;i++) { CharOpt,d;intx, y; Opt=GetChar (); while(opt<'A'|| Opt>'Z') opt=GetChar (); Switch(opt) { Case 'Q': x=read (); y=Read (); Query (x, y); Break; Case 'R': x=Read (); D=getchar (); while(d<'a'|| D>'Z') d=GetChar (); Change (X,D); Break; Case 'I': x=Read (); D=getchar (); while(d<'a'|| D>'Z') d=GetChar (); Insert (X,D); Break; } } return 0;}
[Bzoj 1014] [JSOI2008] Mars man prefix (splay+ +hash)