HDU 4691 (ninth multi-school field 1006) Suffix Array

Source: Internet
Author: User

... What else can I say.

A drop of eyes slide over.

I always thought that the question is the LCP of the current string and all the strings previously entered... Then it's just a whole game.

A glance at the mark suddenly found that he only compared the input string with the previous string?

Suddenly, tens of millions of Grass-mud horses have been in my heart.

Then you can easily get...

Rmq is pre-processed first, and the complexity is nlogn. Then, each LCP query requires only the complexity of O (1.

# Include <set> # include <map> # include <stack> # include <cmath> # include <queue> # include <cstdio> # include <string> # include <vector> # include <iomanip> # include <cstring> # include <iostream> # include <algorithm> # define Max 2505 # define Fi first # define se second # define ll _ int64 # define pi ACOs (-1, 1.0) # define INF 0x3fffffff # define LL (x) (x <1) # define bug puts ("here") # define PII pair <int, int> # define RR (X) (X <1 | 1) # define MP (a, B) make_pair (a, B) # define MEM (a, B) memset (a, B, sizeof ()) # define rep (I, S, T) for (INT I = (s); I <= (t); ++ I) using namespace STD; inline void RD (Int & RET) {char C; int flag = 1; do {c = getchar (); If (C = '-') Flag =-1 ;} while (C <'0' | C> '9'); ret = C-'0'; while (C = getchar ()> = '0' & C <= '9') ret = RET * 10 + (c-'0'); RET * = flag;} inline void ot (int A) {if (a> = 10) ot (A/10); putchar (a % 10 + '0 ');} # define n 1000005/***** suffix array template *****/# define f (x) (X)/3 + (x) % 3 = 1? 0: Tb) // F (x) To find the starting position of suffix (x) of the original string in the new string # define g (x) <TB? (X) * 3 + 1 :( (x)-TB) * 3 + 2) // G (x) is the position of the new string suffix (x) in the original string, and f (x) are reciprocal operations int wa [N], WB [N], WV [N], WS [N]; int SA [N * 3]; int rank1 [N], height [N]; int R [N * 3]; int C0 (int * r, int A, int B) {return R [a] = R [B] & R [A + 1] = R [B + 1] & R [A + 2] = R [B + 2];} int C12 (int K, int * r, int A, int B) {If (k = 2) return R [a] <R [B] | (R [a] = R [B] & C12 (1, R, A + 1, B + 1); else return R [a] <R [B] | (R [a] = R [B] & W V [A + 1] <WV [B + 1]);} void sort (int * r, int * a, int * B, int N, int m) {int I; for (I = 0; I <n; I ++) WV [I] = R [A [I]; for (I = 0; I <m; I ++) WS [I] = 0; for (I = 0; I <n; I ++) WS [wv [I] ++; for (I = 1; I <m; I ++) WS [I] + = ws [I-1]; for (I = n-1; I> = 0; I --) B [-- ws [wv [I] = A [I]; return;} // Note: To facilitate recursive processing, the size of the r array and SA array is 3 * nvoid DC3 (int * r, int * Sa, int N, int m) {// The RN array stores the new recursive string, and the San array is the SA int I of the New String, J, * rn = R + N, * San = SA + N, TA = 0, TB = (n + 1)/3, TBC = 0, P; R [N] = R [n + 1] = 0; for (I = 0; I <n; I ++) {if (I % 3! = 0) wa [TBC ++] = I; // TBC indicates the number of suffixes where the modulo 3 is 1 or 2 at the starting position} Sort (R + 2, WA, WB, TBC, m); sort (R + 1, WB, WA, TBC, m); sort (R, WA, WB, TBC, m); For (P = 1, rn [F (WB [0])] = 0, I = 1; I <TBC; I ++) Rn [F (WB [I])] = C0 (r, WB [I-1], WB [I])? P-1: P ++; If (P <TBC) DC3 (RN, San, TBC, P); else {for (I = 0; I <TBC; I ++) San [Rn [I] = I ;}// sorts all suffixes where the modulo 3 is equal to 0 for (I = 0; I <TBC; I ++) {If (San [I] <TB) WB [ta ++] = San [I] * 3;} If (N % 3 = 1) // n % 3 = 1, special processing of suffix (n-1) WB [ta ++] = n-1; sort (R, WB, WA, Ta, M ); for (I = 0; I <TBC; I ++) WV [WB [I] = g (San [I])] = I; // merge the sorting results of all suffixes and save them in the SA array for (I = 0, j = 0, P = 0; I <TA & J <TBC; P ++) SA [p] = C12 (WB [J] % 3, R, wa [I], WB [J])? Wa [I ++]: WB [J ++]; for (; I <ta; P ++) SA [p] = wa [I ++]; (; j <TBC; P ++) SA [p] = WB [J ++]; return ;}// height [I] = suffix (SA [I-1]) the longest public prefix of suffix (SA [I]), that is, the longest public prefix of the two adjacent suffixes void calheight (int * r, int * Sa, int N) {int I, j, k = 0; for (I = 1; I <= N; I ++) rank1 [SA [I] = I; for (I = 0; I <n; height [rank1 [I ++] = k) for (K? K --: 0, j = sa [rank1 [I]-1]; R [I + k] = R [J + k]; k ++ );} int rmq [N]; int mm [N]; int best [20] [N]; void initrmq (int n) {int I, j, A, B; for (Mm [0] =-1, I = 1; I <= N; I ++) mm [I] = (I & (I-1 )) = 0 )? Mm [I-1] + 1: Mm [I-1]; for (I = 1; I <= N; I ++) best [0] [I] = I; for (I = 1; I <= mm [N]; I ++) for (j = 1; j <= n + 1-(1 <I); j ++) {A = Best [I-1] [J]; B = Best [I-1] [J + (1 <(I-1)]; If (rmq [a] <rmq [B]) best [I] [J] = A; else best [I] [J] = B;} return;} int askrmq (int A, int B) {int T; T = mm [B-A + 1]; B-= (1 <t)-1; A = Best [T] [a]; B = Best [T] [B]; return rmq [a] <rmq [B]? A: B;} int LCP (int A, int B) {int t; A = rank1 [a]; B = rank1 [B]; If (A> B) {T = A; A = B; B = T;} return (height [askrmq (a + 1, B)]);} /*************************************** * *****/# define n 1000005 char a [n]; int N; int CAL (INT now) {If (now = 0) return 1; int nn = 0; while (now) {NN ++; now/= 10 ;} return nn;} int main () {int TTT = 0; while (scanf ("% s", )! = EOF) {int L = strlen (a); For (INT I = 0; I <L; I ++) R [I] = A [I]; R [l] = 0; DC3 (R, SA, L + 1,200); calheight (R, SA, L); For (INT I = 1; I <= L; I ++) rmq [I] = height [I]; initrmq (l); rd (n); LL num1 = 0, num2 = 0; int X, Y; int prex =-1, prey =-1; while (n --) {RD (x); rd (y); num1 + = (Y-x) + 1; if (prex =-1) {num2 + = (Y-x) + 3; prex = x; prey = y; continue;} int now = 0; if (x = prex) {now = min (Prey-prex, Y-x);} else {now = LCP (prex, X ); now = min (Prey-prex, min (Y-X, now);} prex = x; prey = y; int FK = now; now = Y-X-now; if (now = 0) {num2 + = 2 + CAL (FK);} else num2 + = CAL (FK) + now + 2 ;} printf ("% i64d % i64d \ n", num1, num2); TTT ++;} return 0 ;}

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.