BZOJ1014:[JSOI2008] Mars man prefix

Source: Internet
Author: User

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.

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 shown below
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 word
The length of the character string.
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, the word
Inserted at the beginning of the symbol 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 InputMadamimadam
7
Q 1 7
Q 4 8
Q 10 11
R 3 A
Q 1 7
I Ten A
Q 2Sample Output5
1
0
2
1HINT

1. All strings are composed of lowercase letters from beginning to finish.

2, m<=150,000

3, String length L meet 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.

All the way to finally put this problem a ...

Exercises

Use splay to maintain hash of each interval when inserting and modifying (see Code for specific update operations)

LCQ can be two points to find out: each time a two-point answer, the judgment is rotated to see if the hash value of the two interval is the same

Attention:

1.update an interval hash value do not forget to first assign 0 (I was written with the pointer)

2. Do not forget the Getpow Ah, the examination is not easy to find out, I think you are right

3. As a point has been added to the front and back, if the answer to the x=y will be more than 1 of the standard answers, need to special sentence

4. Ask operation X may be greater than Y, to be sentenced

5.bzoj can not use Cin,bzoj can not use Cin,bzoj can not use cin! (important thing to say three times!!!) For this I am full of the re ah ... It's all tears. 555)

Code:

#include <cstdio>#include<iostream>#include<string.h>using namespacestd;Const intmaxn=140005;structnode{intd,size; inth; Node*parent,*ch[2]; }POOL[MAXN],*rf,*Root;intCNT,POW[MAXN];voidGetpow () {pow[0]=1;  for(intI=1; i<maxn;i++) Pow[i]=(Long Long) pow[i-1]*137%10000007;}intSize (node *p) {    if(p)returnP->size; return 0;}voidUpdate (Node *p) {P->size=1+size (p->ch[0]) +size (p->ch[1]); P->h=0; if(p->ch[0]) p->h=p->ch[0]->h; P->h= ((Long Long) p->h*pow[1]+P-&GT;D)%10000007; if(p->ch[1]) p->h= (Long Long) P->h*pow[size (p->ch[1])]+p->ch[1]-&GT;H)%10000007;}voidRotate (node *p,inttype) {Node*parent=p->parent,*son=p->ch[!type],*gp=p->parent->parent; Parent->ch[type]=Son; if(son) son->parent=parent; P->ch[!type]=parent; Parent->parent=p; P->parent=GP; GP->ch[parent==gp->ch[1]]=p;    Update (parent);    Update (P); if(parent==root) root=p;}voidSplay (Node *p,node *target) {     while(p->parent!=target) {        if(p->parent->parent==target) rotate (p,p==p->parent->ch[1]); Else{node*parent=p->parent,*gp=p->parent->parent; intf=parent==gp->ch[0]; if(P==parent->ch[f]) rotate (p,f), rotate (p,!f); ElseRotate (parent,!f), rotate (p,!f); }}}node*find (Node *p,intk) {    if(Size (p->ch[0]) >=k)returnFind (p->ch[0],k); Else if(Size (p->ch[0]) ==k-1)returnp; Else returnFind (p->ch[1],k-1-size (p->ch[0])); }voidInsertintX,node *newnode) {Node*p=find (ROOT,X);    Splay (P,RF); P=find (root,x+1);    Splay (P,root); P->ch[0]=NewNode; NewNode->parent=p;    Update (P); Update (P-parent);}voidChangeintXintd) {Node*p=find (ROOT,X);    Splay (P,RF); P->d=D; Update (P);}BOOLQueryintXintYintdis) {    if(dis==-1)return true; Node*p=find (root,x-1);    Splay (P,RF); P=find (root,x+dis+1);    Splay (P,root); inthash1=p->ch[0]->h; P=find (root,y-1);    Splay (P,RF); P=find (root,y+dis+1);    Splay (P,root); if(p->ch[0]-&GT;H==HASH1)return true; return false;}intQuery (intXinty) {    if(x>y) Swap (x, y); ints=-1, T=size (Root)-y-1, Mid;  while(s<t) {mid= (s+t+1)/2; if(Query (x,y,mid)) s=mid; Elset=mid-1; }    returnS+1;}intMain () {Charch1[100000],sh[1],sh1[1]; intI,m,x,y,len; Node*p; scanf ("%s", CH1); Len=strlen (CH1);    Getpow (); RF=&pool[++CNT]; Root=&pool[++CNT]; Root->d=root->h=0; Root->size=1; Root->parent=RF; RF->ch[0]=Root;  for(i=0; i<len;i++) {p=&pool[++CNT]; P->d=p->h=ch1[i]-'a'+1; P->size=1; Root->ch[1]=p; P->parent=Root;    Splay (P,RF); } P=&pool[++CNT]; P->d=p->h=0;p->size=1; Root->ch[1]=p; P->parent=Root;        Splay (P,RF); scanf ("%d",&m);  while(M--0) {scanf ("%s", SH); if(sh[0]=='Q') {scanf ("%d%d",&x,&y); X++;y++; if(x!=y) printf ("%d\n", Query (x, y)); Elseprintf"%d\n", size (Root)-x); }        Else if(sh[0]=='R') {scanf ("%d", &x); scanf ("%s", SH1); X++; Change (x,sh1[0]-'a'+1); }        Else{scanf ("%d", &x); scanf ("%s", SH1); X++; P=&pool[++CNT]; P->d=p->h=sh1[0]-'a'+1; P->size=1;        Insert (X,P); }    }        return 0;}

BZOJ1014:[JSOI2008] Mars man prefix

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.