[bzoj]1014 Mars man prefix (JSOI2008)

Source: Internet
Author: User

  While listening to the provincial team Dalao lectures while doing real TM stimulation.

Bzoj's discuss is simply a combination of surface plus, bulk case, Superhint and Dalao.

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: Madamimadam, we label each character of the 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 means: The string that starts with the first x character of the substring, and the string that starts with the first character
The length of the public prefix of two strings. Let's say, LCQ (1, 7) = 5, LCQ (2, ten) = 1, LCQ (4, 7) = 0 During the study of the LCQ function, the Martians discovered an association: if all suffixes of the string are ordered, the value of the LCQ function can be obtained quickly, and if the LCQ function is obtained Value, the suffix of the string can also be sorted quickly. 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 a string, or you can insert a character at one point in the string. Earth people want to test, in such a complex question, whether the Martians can also be able to quickly find the value of the LCQ function.

Input

The first line gives the initial string. The second line is a non-negative integer m that represents the number of operations. The next M-line, each line describes an operation. Exercise
There are 3 types, as follows:
1, inquiry. Syntax: Qxy,x,y are positive integers. Function: Calculates LCQ (x, y) limit: 1<=x,y<= the current string length.
2. Modification. Syntax: Rxd,x is a positive integer and D is a character. Function: Modifies the number of x in the string to character D. Limit: x does not exceed the current string length.
3, insert: Syntax: Ixd,x is a non-negative integer, D is a character. Function: Inserts the character d after the string x character, and if x=0, inserts at the beginning of the string. Limit: x does not exceed the current string length.

Output

For each query in the input file, you should output the corresponding answer. One answer line.

Sample Input

Madamimadam
7
Q 1 7
Q 4 8
Q 10 11
R 3 A
Q 1 7
I Ten A
Q 2 11

Sample Output

5
1
0
2
1

HINT

1. All strings are composed of lowercase letters from beginning to finish.
2, m<=150,000.
3. The string length L satisfies l<=100,000 from beginning to finish.
4, the number of inquiry operation is not more than 10,000.
For the 1th, 2 data, the string length does not exceed 1,000 from beginning to the other.
For the 3,4,5 data, there is no insert operation.

Solution

For this problem, according to the inertial thinking we should first think of is the suffix array bar, after all, the title described directly in the LCP.

Can you look at it and change it dynamically? Also to support the insertion?? Obviously the suffix array is not realistic.

  Sure enough, the people in the topic wrote what algorithm we can not write anything.

We start with the insertion operation, and soon we think of the balance tree;

We start by judging whether the substring is equal, and soon we think of the string hash. (Brain hole source noi2016d1t1)

Then the problem is done.

We use the balance tree to maintain the hash value of the substring, ask the two-point answer, each time to determine whether the hash value of two substrings is equal.

Note that the hash can be natural overflow, small c wrote a merge log hash, and opened a long long, run extremely slow. (There is a better method of hashing do not learn small C.)

(small C's) total time complexity. (q is the number of queries, M is the number of revisions)

 

#include <cstdio>#include<cstring>#include<algorithm>#defineL (a) son[a][0]#defineR (a) son[a][1]#defineRT (L (RTFA))#defineRTFA (MN-1)#defineMN 100005#definell Long Longusing namespacestd;intsiz[mn],son[mn][2],CR[MN],FA[MN];CharC[MN];intdin,n,m;ll Hs[mn];inlineintRead () {intn=0, f=1;CharC=GetChar ();  while(c<'0'|| C>'9') {if(c=='-') f=-1; C=GetChar ();}  while(c>='0'&& c<='9') {n=n*Ten+c-'0'; C=GetChar ();} returnNF;} inline ll mi (ll X,inty) {Register LL Z=1;  for(; y;y>>=1, x=x*x)if(y&1) z=z*x; returnZ;} InlinevoidUpdateintx) {Siz[x]=siz[l (x)]+siz[r (x)]+1; HS[X]= (hs[l (x)]*101+CR[X]) *mi (101, Siz[r (x)]) +hs[r (x)];}voidRotateintx) {RegisterintY,z,l,r; Y=FA[X]; Z=fa[y]; L=r (y) ==x; r=l^1; Fa[y]=x; Fa[x]=z; fa[son[x][r]]=y; Son[z][r (z)==y]=x; SON[Y][L]=SON[X][R]; son[x][r]=y; Update (y);}voidSplay (intXintRF) {Registerinty,z;  for(; fa[x]!=rf;rotate (x))if(FA[Y=FA[X]]!=RF) z=fa[y],rotate (R (z) ==y^r (y) ==x?x:y); Update (x);}voidGetins (int&x,intFatintYCharz) {    if(!x) {x=++din; cr[x]=z; fa[x]=fat; splay (X,RTFA);return;} if(Y>=siz[l (x)]+1) Getins (R (x), X,y-siz[l (x)]-1, z); ElseGetins (L (x), z/y);}voidGetsiz (intXintYintRTF) {    if(Y==siz[l (x)]+1) {splay (X,RTF);return;} Else if(Y>siz[l (x)]+1) Getsiz (R (x), Y-siz[l (x)]-1, RTF); ElseGetsiz (L (x), y,rtf);}voidGETITV (intLintR) {r+=2; Getsiz (RT,L,RTFA); Getsiz (RT,R,RT);}voidBuildint&x,intFatintLintR) {    if(L>r)return; intMid=l+r>>1; X=++din; Cr[x]=c[mid]; fa[x]=fat; Build (L (x), X,l,mid-1); Build (R (x), x,mid+1, R); Update (x);}intMain () {Charg[2],z[2]; RegisterintX,y,l,r;    Register ll Lx,ly; scanf ("%s", c+1); N=strlen (c+1); m=read (); c[0]=c[n+1]=0; Build (RT,RTFA,0, n+1);  while(m--) {scanf ("%s", G); if(g[0]=='Q') {x=read (); y=read (); if(x>y) Swap (x, y);  for(l=0, r=n-y+1; l<R;) {                intmid=l+r+1>>1; GETITV (X,x+mid-1); lx=Hs[l (R (RT))]; GETITV (Y,y+mid-1); ly=Hs[l (R (RT))]; if(lx==ly) L=mid;Elser=mid-1; } printf ("%d\n", L); }        Else if(g[0]=='R') {x=read (); scanf"%s", z); Getsiz (rt,x+1, RTFA); CR[RT]=z[0];        Update (RT); }        Else if(g[0]=='I') {x=read (); ++n; scanf ("%s", z); Getins (rt,rtfa,x+1, z[0]);} }}

Last Word

Small C began to do this problem, the idea has been asked to take the person to run.

Then the brain complements some nasty things, suffix array, suffix balance tree, suffix automaton ... No results.

Bored point open discuss, one eye see the title "XXX Natural Overflow xxx", instantly feel oneself is a retarded, turn over this topic cut.

[bzoj]1014 Mars man prefix (JSOI2008)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.