1014: [JSOI2008] mars man prefix
Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 4264 Solved: 1306
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 of characters starting with the first x character of the strings, and the length of the common prefix of the two string, starting with the nth character in the 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 have cleverly found a fast algorithm for finding LCQ functions, the people who are unwilling to concede the earth have a problem with the Martians: they can also change the string itself while taking LCQ functions. 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. There are 3 operations, as follows: 1, inquiry. Syntax: Q x y,x, y are positive integers. Function: Calculates LCQ (x, y) limit: 1 <= x, y <= the current string length. 2. Modification. Syntax: R x d,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: I x d,x is a non-negative integer, D is a character. Function: Inserts the character D after the first X character of the string, and inserts it at the beginning of the string if x = 0. 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
madamimadam7Q 1 7Q 4 8Q 10 11R 3 aQ 1 7I 10 aQ 2 11
Sample Output
51021
HINT
Data size:
对于100%的数据,满足:1、 所有字符串自始至终都只有小写字母构成。2、 M <= 150,0003、 字符串长度L自始至终都满足L <= 100,0004、 询问操作的个数不超过10,000个。对于第1,2个数据,字符串长度自始至终都不超过1,000对于第3,4,5个数据,没有插入操作。
Exercises
String hash.
Classic binary +hash, because there are insertions and modifications, with the balance of the tree to maintain a bit better
PS: Next time be sure to write splay.
Code:
#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <cstring>#include <algorithm>using namespace STD;#define N 200100structnode{node *l,*r;intSz,cnt,p;unsigned intVCharC;} *root,*null=NewNode (node) {null,null,0,0,0,0,}), chi[250100];intm,len,num=0;CharS[n];unsigned intMi[n];typedefPair<node*,node*> Dnode;inline intIn () {intx=0;CharCh=getchar (); while(ch<' 0 '|| Ch>' 9 ') Ch=getchar (); while(ch>=' 0 '&& ch<=' 9 ') x=x*Ten+ch-' 0 ', Ch=getchar ();returnx;}inline voidPUSH_UP (node *x) {x->sz=x->l->sz+x->r->sz+x->cnt; x->v= (int) (X->C)- the;if(X->l->sz &&!x->r->sz) x->v=x->l->v+x->v*mi[x->l->sz];Else if(!x->l->sz && X->r->sz) x->v=x->v+x->r->v*mi[1];Else if(X->l->sz && X->r->sz) x->v=x->l->v+x->v*mi[x->l->sz]+x->r->v*mi[x-& GT;L->SZ+X->CNT];}inlineNode *merge (node *x,node *y) {if(!X->SZ)returnYif(!Y->SZ)returnXif(x->p<y->p) {X->r=merge (x->r,y); PUSH_UP (x);returnX }Else{Y->l=merge (x,y->l); Push_up (y);returnY }}inlineDnode Split (node *x,intK) {if(!X->SZ)returnDnode (Null,null); Dnode y;if(K<=X->L->SZ) {y=split (x->l,k); x->l=y.second; PUSH_UP (x); Y.second=x; }Else{y=split (x->r,k-x->l->sz-x->cnt); x->r=y.first; PUSH_UP (x); Y.first=x; }returnY;}inline voidInsert (intWCharc) {Dnode x=split (root,w); Node *t=&chi[num++]; t->l=t->r=null; T->sz=t->cnt=1; t->c=c; t->v= (int) (c)- the; T->p=rand (); X.first=merge (x.first,t); Root=merge (X.first,x.second);}inline voidDelete (intW) {Dnode x, y; X=split (root,w-1); Y=split (X.second,1); Root=merge (X.first,y.second);}inline voidQueryintXintY) {intL=1, R=min (len-x+1, len-y+1), ans=0;unsigned intK1,K2; Dnode A, B; while(L<=R) {intMid= (l+r) >>1; A=split (root,x-1), B=split (A.second,mid); PUSH_UP (B.first); k1=b.first->v; A.second=merge (B.first,b.second); Root=merge (A.first,a.second); A=split (root,y-1), B=split (A.second,mid); PUSH_UP (B.first); k2=b.first->v; A.second=merge (B.first,b.second); Root=merge (A.first,a.second);if(K1==K2) l=mid+1, Ans=mid;Elser=mid-1; }printf("%d\n", ans);}intMain () {scanf('%s ', s); len=strlen(s); M=in (); mi[0]=1; Root=null; for(intI=1; i<=n; i++) mi[i]=mi[i-1]* in; Node *x; for(intI=0; i<len; i++) {x=&chi[num++]; x->cnt=x->sz=1; x->l=x->r=null; x->c=s[i]; x->v= (int) (X->C)- the; X->p=rand (); Root=merge (ROOT,X); } while(m--) {CharOpt=getchar (); while(opt!=' Q '&& opt!=' R '&& opt!=' I ') Opt=getchar ();intx, y;CharCSwitch(opt) { Case ' Q ': X=in (), Y=in (); Query (x, y); Break; Case ' R ': {x=in (), C=getchar (); while(c<' A '|| C>' Z ') C=getchar (); Delete (x), Insert (-X1, c); Break; } Case ' I ': {x=in (), C=getchar (); while(c<' A '|| C>' Z ') C=getchar (); Insert (X,C); len++; Break; } } }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"Dichotomy +hash" "string" "Balance Tree" "Jsoi 2008" Mars Man Prefix