HDU 4622 Reincarnation (length of any interval substring, suffix array +rmq)

Source: Internet
Author: User

The main topic: give you a string, give you a query n times, each time you give you an interval to find out how many substrings in this interval.

Problem-solving ideas: We must enumerate the position, then find the common substring and then remove the duplicate, but his address corresponding to rank is not continuous, if the violence to find the words will n*n time out.

From this blog to learn a method: first to the entire string sa[] and height[], then for any interval [L, R], traverse through sa[], as long as the starting point in [L, r] suffix needs to be counted, similar to 1) in the method, But one place to pay special attention is that all sa[] is not necessarily the sa[in the interval, because there is an extra length limit when the suffix in the interval is compared. You can prove that the traversal process follows the following rules:


suffix s1 and suffix S2 are now two suffixes to be compared, S1 before, S2 in the interval [L, R], and set two strings in the interval length of LEN1, Len2, its global longest public prefix is LCP. Now consider how to get the correct local sa[from the global sa[] when traversing sa[]:
1:LCP < len1 && LCP < len2 the two strings are compared at the end of the size, global and local sa[] unified, so you can rest assured that S2 as the next dictionary suffix;
2:LCP >= len1 && LCP >= Len2 When the end of one of the strings, two string corresponding characters are equal, then need to be based on len1 and len2 relationship to decide, if the len1>len2 then do not have to replace;
3:LCP >= len1 && LCP < len2 description at the end of one of the strings, the corresponding characters of two strings are equal, because the S2 length is longer than S1, so the dictionary order is certainly large, so the current suffix needs to be replaced;
4:LCP < len1 && LCP >= len2 Description at the end of one of the strings, the corresponding characters of two strings are equal, because the S1 length is longer than S2, so the dictionary order is definitely large, so the current suffix is not required.
where 2 and 4 conditions can be combined, if 4 is established, then there must be Len1 > len2, so this judgment can be simplified: if (len1 > Len2 && LCP >= Len2) The else replacement is not replaced.
if (La > lb && LCP >= lb) {do not swap} else {swap};

The direct understanding is that the result of error is only the case that a given LCP completely contains the suffix is placed in the back, then its correct position should be at the front, because the length of the match is still not the length of the entire local suffix, and in the selection of the next dictionary suffix when the suffix can be masked out.

Reincarnation Time limit:6000/3000 MS (java/others) Memory limit:131072/65536 K (java/others)
Total submission (s): 2277 Accepted Submission (s): 795


Problem Descriptionnow You is Back,and has a task to do:
Given you a string s consist of lower-case 中文版 letters Only,denote F (s) as the number of distinct sub-string of S.
And you have some query,each time should calculate f (s[l ... R]), S[l ... R] means the sub-string of S start from L end at R.
Inputthe first line contains integer T (1<=t<=5), denote the number of the test cases.
For each test cases,the first line contains a string s (1 <= length of S <= 2000).
Denote the length of s by N.
The second line contains an integer Q (1 <= q <= 10000), denote the number of queries.
Then Q lines Follows,each lines contains the integer l, r (1 <= l <= r <= N), denote a query.
Outputfor each test cases,for query,print the answer on one line.
Sample Input
2bbaba53 4baaba53 33 41 43 55 5

Sample Output
3175813851Hint      I won ' t do anything against hash because I am nice. Of course this problem have a solution that don ' t rely on hash.

Source2013 multi-university Training Contest 3
#include <algorithm> #include <iostream> #include <stdlib.h> #include <string.h> #include < iomanip> #include <stdio.h> #include <string> #include <queue> #include <cmath> #include < stack> #include <ctime> #include <map> #include <set> #define EPS 1e-9///#define M 1000100///#define ll __int64#define ll Long long///#define INF 0x7ffffff#define inf 0x3f3f3f3f#define PI 3.1415926535898#define Zero (x) ((FA BS (x) <eps)? 0:x) #define MOD 1000000007#define Read () freopen ("Autocomplete.in", "R", stdin) #define Write () freopen ("    Autocomplete.out "," w ", stdout) #define CIN () Ios::sync_with_stdio (false) using namespace Std;inline int Read () {char ch;    BOOL flag = FALSE;    int a = 0; while (!) ( ((ch = getchar ()) >= ' 0 ') && (ch <= ' 9 ')) | |    (ch = = '-')));        if (ch! = '-') {a *= 10;    A + = ch-' 0 ';    } else {flag = true; } while (((ch = getchar ()) >= ' 0 ') && (ch <= ' 9 ')) {        A *= 10;    A + = ch-' 0 ';    } if (flag) {a = A; } return A;        void write (int a) {if (a < 0) {Putchar ('-');    A =-A;    } if (a >=) {write (A/10); } putchar (a% 10 + ' 0 ');} const int MAXN = 2050;int WA[MAXN], WB[MAXN], WV[MAXN], ws1[maxn];int sa[maxn];int cmp (int *r, int A, int b, int l) {RE Turn R[a] = = R[b] && r[a+l] = = R[b+l];}    void da (int *r, int *sa, int n, int m) {int I, J, p, *x = WA, *y = WB;    for (i = 0; i < m; i++) ws1[i] = 0;    for (i = 0; i < n; i++) ws1[x[i] = r[i]]++;    for (i = 1; i < m; i++) ws1[i] + = ws1[i-1];    for (i = n-1; I >= 0; i--) sa[--ws1[x[i]] = i;        for (j = 1, p = 1; p < n; j <<= 1, m = p) {for (P = 0, i = n-j; i < n; i++) y[p++] = i;        for (i = 0; i < n; i++) if (Sa[i] >= j) y[p++] = sa[i]-j;        for (i = 0; i < n; i++) wv[i] = X[y[i];        for (i = 0; i < m; i++) ws1[i] = 0; for (i = 0; i < n; i++) ws1[wv[i]]++;        for (i = 1; i < m; i++) ws1[i] + = ws1[i-1];        for (i = n-1; I >= 0; i--) sa[--ws1[wv[i]] = y[i];    for (Swap (x, y), p = 1, x[sa[0]] = 0, i = 1; i < n; i++) x[sa[i]] = cmp (y, sa[i-1], Sa[i], j)? p-1:p++;    }}int RANK[MAXN], height[maxn];void calheight (int *r, int *sa, int n) {int I, j, k = 0;    for (i = 1; I <= n; i++) rank[sa[i]] = i;    for (int i = 0; i < n; height[rank[i++]] = k) for (k?k--:0, j = sa[rank[i]-1]; r[i+k] = = R[j+k]; k++); return;}    int dp[maxn][30];void RMQ (int len) {for (int i = 1; I <= len; i++) dp[i][0] = Height[i];  for (int j = 1; 1<<j <= Maxn; j + +) {for (int i = 1; i+ (1<<j)-1 <= len; i++) Dp[i][j]    = Min (dp[i][j-1], dp[i+ (1<< (j-1))][j-1]);    }}int lg[maxn];int querry (int l, int r) {int k = lg[r-l+1]; return min (Dp[l][k], dp[r-(1<<k) +1][k]);}    void Init () {lg[0] =-1; for (int i = 1; i < MAXN; ++i) lg[i] = lg[i&GT;&GT;1] + 1;}    Char str[maxn];int seq[maxn];int Del (int l, int r, int n) {int ans = (r-l+2) * (r-l+1)/2;    int last =-1;    int k = r-l+1;        for (int i = 1; I <= n; i++) {if (!k) break;        if (Sa[i] < L | | sa[i] > R) continue;        k--;            if (last = =-1) {last = i;        Continue        } int a = last;        int b = i;        if (a > B) Swap (A, b);        int LCP = Querry (a+1, b);        int la = r-sa[last]+1;        int lb = r-sa[i]+1;        if (La > lb && LCP >= lb) {} else last = i;    Ans-= min (LCP, min (LA, lb)); } return ans;    int main () {int T;    CIN >>T;    Init ();        while (t--) {scanf ("%s", str);        int len = strlen (str);        for (int i = 0; i < len; i++) seq[i] = str[i]-' a ' +1;        Seq[len] = 0;        Da (seq, SA, len+1, 27);        Calheight (seq, SA, Len);        RMQ (len);        int n;        int L, R;        scanf ("%d", &n); WhIle (n--) {scanf ("%d%d", &l, &r);        printf ("%d\n", Del (L-1, R-1, Len)); }} return 0;}



HDU 4622 Reincarnation (length of any interval substring, suffix array +rmq)

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.