HDU 4691 front compression (suffix array LCA)

Source: Internet
Author: User

Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4691

The question can only be TLE or MLE.

If the incorrect question is the prefix of all the previous inserts, use the suffix array TLE

If trie MLE is used, the problem cannot be solved even if it is wrong.

In fact, the positive solution is similar to the above input.

Two Methods: suffix array combined with rmq brute force water

When using rmq, note that N is included during initialization.

L ++ during query, because height is the current and the longest prefix.

Suffix array:

# Include <iostream> # include <stdio. h> # include <algorithm> # include <math. h> # include <string. h> using namespace STD; # define maxn 110000 # define ws ws1int wa [maxn], WB [maxn], WV [maxn], WS [maxn]; int CMP (int * r, int A, int B, int L) {return R [a] = R [B] & R [A + L] = R [B + L];} void da (char * r, int * Sa, int N, int m) {int I, j, P, * x = wa, * Y = WB, * t; for (I = 0; I <m; I ++) WS [I] = 0; for (I = 0; I <n; I ++) WS [x [I] = R [I] ++; for (I = 1; I <m; I ++) WS [I] + = WS [I-1]; for (I = n-1; I> = 0; I --) SA [-- ws [x [I] = I; for (j = 1, P = 1; P <n; j * = 2, 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 ++) 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 --) sa [-- ws [wv [I] = Y [I]; for (t = x, x = Y, y = T, 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 ++;} return;} int rank [maxn], height [maxn]; void calheight (char * r, int * Sa, int N) {int I, j, k = 0; for (I = 1; I <= N; I ++) rank [SA [I] = I; // calculate the rank when calculating the height, and use for (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 ;} char rec [maxn]; int SA [maxn]; int N; int rmq [maxn] [20]; int rmq (int n) {int I, J; for (I = 0; I <= N; I ++) rmq [I] [0] = I; for (j = 1; (1 <j) <= N; j ++) for (I = 0; I + (1 <j)-1 <= N; I ++) {If (height [rmq [I] [J-1] <= height [rmq [I + (1 <(J-1)] [J-1]) rmq [I] [J] = rmq [I] [J-1]; elsermq [I] [J] = rmq [I + (1 <(J-1)] [J-1];} return 0;} int query (int l, int R) {If (L> r) L ^ = R, R ^ = L, L ^ = R; if (L! = R) l ++; int K = Log (double (R-l + 1)/log (2.0 ); if (height [rmq [l] [k] <= height [rmq [R-(1 <k) + 1] [k]) return rmq [l] [k]; elsereturn rmq [R-(1 <k) + 1] [k];} int find_ans (int K) {If (k <10) return 1; int ans = 0; while (k) {ans ++; k/= 10;} return ans;} int main () {int I, J, K, a, B, c, d, num; int mid, M; long ans1, ans2; while (scanf ("% s ", REC )! = EOF) {n = strlen (REC); ans1 = ans2 = 0; DA (REC, SA, N + 1,200 ); // here is n + 1 because the function contains <n calheight (REC, SA, N ); // note that N is used here. Because N is used in the function, note the following: // for (I = 0; I <n; I ++) // printf ("% d", height [I]); // printf ("\ n"); rmq (n); scanf ("% d ", & M); scanf ("% d", & A, & B); C = A, D = B; ans1 = B-A + 1; ans2 = B-A + 2 + 1; for (I = 1; I <m; I ++) {scanf ("% d", &, & B); If (rank [a]! = Rank [c]) {num = query (rank [a], rank [c]); num = height [num]; If (Num> B-) num = B-A; If (Num> D-C) num = D-C;} else {num = min (B-a, d-C );} ans1 + = B-A + 1; ans2 + = (B-a-num + 2) + find_ans (Num); C = A, D = B; // printf ("Ans: % d \ n", num);} printf ("% i64d % i64d \ n", ans1, ans2);} return 0 ;}

Brute force:

#include <iostream>#include <string.h>#include <stdio.h>#include <cmath>#include <algorithm>using namespace std;#define maxn 510000char str[maxn];int find_ans(int k){    if(k<10)    return 1;    int ans=0;    while(k)    {        ans++;        k/=10;    }    return ans;}int find_max(int a,int b,int c,int d){    int ans=0;    int i,j;    for(i=a,j=c;i<b && j<d;i++,j++)    if(str[i]==str[j])    ans++;    else    return ans;    return ans;}int main(){    long long ans1,ans2;    int n,a,b,len1,len2,c,d,num,i,j,k;    while(scanf("%s",str)!=EOF)    {        ans1=ans2=0;        scanf("%d",&n);        c=0,d=0;        for(i=0;i<n;i++)        {            scanf("%d%d",&a,&b);            ans1+=b-a+1;            if(i==0)            {                ans2+=b-a+2+1;            }            else if(c==a)            {                num=min(b-a,d-c);// printf("ans:%d %d\n",num,find_ans(num));                ans2+=(b-a-num+2)+find_ans(num);            }            else            {                num=find_max(a,b,c,d);                ans2+=(b-a-num+2)+find_ans(num);            }            c=a,d=b;        }        prinf("%I64d %I64d\n",ans1,ans2);        //cout<<ans1<<' '<<ans2<<endl;    }    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.