[bzoj]1014 [jsoi]2008 Martian prefix splay

Source: Internet
Author: User

1014: [JSOI2008] Martian prefix

Time Limit:10 Sec Memory limit:162 MB
submit:8170 solved:2592
[Submit] [Status] [Discuss]
Description

The Martians have recently studied an operation: to find a common prefix for a string of two suffixes. For example, there is such a string: Madamimadam,
We label each character of this string: serial number: 1 2 3 4 5 6 7 8 9 10 11 characters m A d a m i m a D a m now,
The Martians define a function, LCQ (x, y), that is, the string in which the first X character begins, and the string that starts with the Y character
The length of the public prefix of the two string. For example, LCQ (1, 7) = 5, LCQ (2, ten) = 1, LCQ (4, 7) = 0 in the process of studying the LCQ function
, the Martians found such an association: if all the suffixes of the string were sorted out, the value of the LCQ function could be quickly calculated;
If you find the value of the LCQ function, you can also quickly order the suffix of the string. Although the Martians were smart enough to find a quick LCQ function
algorithm, but unwilling to give up the Earth people have given the Martians a problem: While the LCQ function can be obtained, but also to change the string itself. Specifically
, you can change the value of one character in a string, or you can insert a character at one point in the string. The earth people want to test, in such
In complex problems, whether the Martians are able to get the value of the LCQ function very quickly.

Input

The first line gives the initial string. The second line is a nonnegative integer m that represents the number of operations. The next line of M, each line describes an operation. Exercise
There are 3 kinds, as shown below
1, inquiry. Syntax: Qxy,x,y are all positive integers. Functions: Computes the LCQ (x,y) limit: 1<=x,y<= the current string length.
2, modify. 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 current word
Character string length.
3, insert: Syntax: Ixd,x is a nonnegative integer, D is a character. Function: Inserts the character d after the string x character, if x=0, then in the word
Inserts at the beginning of the string. Limit: x does not exceed current string length

Output

For each query operation in the input file, you should output the corresponding answer. An 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 only lowercase letters from start to finish.

2, m<=150,000

3, String length L always meet l<=100,000

4, ask the number of operations not more than 10,000.

For the 1th, 2 data, the string length does not exceed 1,000

There is no insert operation for the 3,4,5 data.

Source

[Submit] [Status] [Discuss]

Home back

Correct fingering practice a little bit more familiar, can write a problem ... It's not too long before you're proficient.
Originally also thought to use what suffix balance tree ... Sure enough to naive, string matching can also use hash ah ... Because the insert description requires the dynamic maintenance of the hash value, we think of the balance tree. Because of the longest prefix, we also need a binary prefix length to see if the prefix hash value is the same to find the longest prefix-this means that we need to dynamically and quickly find a string of hash value, then it is splay!! Using splay to maintain the hash value dynamically, each extraction interval is a forte.
Use% good slow ah, card time past ... The natural overflow of the unsigned is one-fold faster.

#include <stdio.h> #include <cstring> #include <algorithm> using namespace std;
#define BOC Register char #define ACCE register int unsigned int mod = 137;
const int MAXN = 150015;
int n, m, tot, root;
Char ex[2], ss[2], S[MAXN];
int PW[MAXN], FA[MAXN], VV[MAXN];
int SIZ[MAXN], c[maxn][2];
unsigned int v[maxn], H[MAXN];
    inline void init () {pw[0] = 1;
for (int i = 1; i < MAXN + i) pw[i] = pw[i-1] * MOD;
    } Inline const int read () {acce x = 0;
    Boc ch = getchar ();
    while (Ch < ' 0 ' | | | ch > ' 9 ') ch = getchar ();
    while (Ch >= ' 0 ' && ch <= ' 9 ') x = (x << 3) + (x << 1) + CH-' 0 ', ch = getchar ();
return x;
    } inline void Update (int x) {int L = c[x][0], r = c[x][1];
    SIZ[X] = Siz[l] + siz[r] + 1;
H[X] = H[l] + v[x] * Pw[siz[l]] + h[r] * Pw[siz[l] + 1];
    } inline void rotate (int x, int &wanna) {int y = fa[x], z = fa[y];
    int L = (c[y][0]!= x), r = l ^ 1; (Y!= Wanna)? C[Z][C[Z][0] != y] = X:wanna = x;
    FA[X] = Z, fa[y] = x, fa[c[x][r]] = y;
    C[Y][L] = C[x][r], c[x][r] = y;
Update (y), update (x); } inline void splay (int x, int &wanna) {for (int f; x!= wanna; rotate (x, Wanna)) if ((f = fa[x))!= Wann
A) rotate ((c[fa[f]][0] = = f ^ c[f][0] = = x)? x:f, Wanna); void Insert (int &x, int k, int f, int fix) {if (!x) {x = + + tot, fa[x] = f, siz[x] = 1, v[x] = h[x
        ] = fix; Splay (x, root);
    Return
    } if (k >= siz[c[x][0]] + 1) Insert (c[x][1], k-siz[c[x][0]]-1, x, fix);
else Insert (c[x][0], K, x, fix);
    int find (int x, int k) {if (siz[c[x][0]] + 1 = k) return x;
    else if (Siz[c[x][0]] >= k) return find (C[x][0], k);
else return find (c[x][1], k-siz[c[x][0]]-1);
    } void Build (int &x, int f, int L, int R) {if (L > R) return;
    int mid = (L + R) >> 1;
    x = + + tot;
    Fa[x] = f, v[x] = Vv[mid]; Builds (c[x][0], X, L, mid-1), Build (C[x][1), X,Mid + 1, R);
Update (x);
    inline int Calc (int x, int len) {splay (Find (root, x), root);
    Splay (Find (root, x + len + 1), c[root][1]);
return h[c[c[root][1]][0]];
    inline int query (int x, int y) {int lf = 1, rg = min (tot-x, tot-y)-1, ans = 0;
        while (LF <= RG) {int mid = (LF + RG) >> 1;
        if (Calc (x, mid) = = Calc (y, mid)) LF = mid + 1, ans = mid;
    else RG = mid-1;
return ans;
    int main () {init ();
    scanf ("%s", S + 2);
    n = strlen (S + 2), M = Read ();
    S[1] = s[n + 2] = ' a ';
    for (Acce i = 1; I <= n + 2; + + i) vv[i] = s[i]-' a ' + 1;
    Build (Root, 0, 1, n + 2);
        for (Acce i = 1; I <= m + i) {scanf ("%s", SS);
        int x = read ();
            if (ss[0] = = ' Q ') {int y = read ();
            printf ("%d\n", query (x, y));
        Continue
        } scanf ("%s", ex); if (ss[0] = = ' R ') {x = find (root, x + 1), splay (x, ROOT);
        V[root] = ex[0]-' a ' + 1, update (root);
    if (ss[0] = = ' I ') insert (root, x + 1, 0, ex[0]-' a ' + 1); }
}

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.