Lightoj-1347 Aladdin and the magical Lamp (suffix array && segment tree)

Source: Internet
Author: User
Tags cas min strlen
Title: http://lightoj.com/volume_showproblem.php?problem=1347
Test Instructions:Find the longest common substring of three strings Ideas:

The total length of three strings is not more than 150000 so you can use a suffix array to treat them as a string.

Concatenate three strings into a string and then run through the suffix array, sort by rank[] and find a range of substrings containing three strings height[] The minimum value of the array is an answer, and finding the maximum value of all these intervals is the final answer.

The suffix array with da or DC3 difference is not small because the enumeration interval after the scale of the complexity of O (n), the interval minimum value with the line segment tree is O (logn), so the total complexity must be O (N*LOGN). Code:

Kopyh #include <bits/stdc++.h> #include <stdio.h> #define INF 0x3f3f3f3f #define MOD 1000000007 #define N 50
0100 using namespace Std;
int N,m,res,flag;
Char S[n];
int seq[n], sa[n], ranks[n], height[n];
int wwa[n], wwb[n], wws[n], wwv[n];  BOOL CMP (int r[], int A, int b, int l) {return r[a] = = R[b] && r[a+l] = = R[b+l];} void da (int r[],int n, int
    m) {int I, J, p, *x = WWA, *y = WWB;
    for (i = 0; i < m; ++i) wws[i] = 0;
    for (i = 0; i < n; ++i) wws[x[i]=r[i]]++;
    for (i = 1; i < m; ++i) wws[i] + = wws[i-1];
    for (i = n-1; I >= 0; i.) sa[--wws[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) wwv[i] = X[y[i];
        for (i = 0; i < m; ++i) wws[i] = 0;
        for (i = 0; i < n; ++i) wws[wwv[i]]++; for (i = 1; i < m; ++i) wws[i] + = WWS[I-1];
        for (i = n-1; I >= 0; i) sa[--wws[wwv[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++;
    }} void Calheight (int r[], int n) {int I, j, k = 0;
    for (i = 1; I <= n; ++i) ranks[sa[i]] = i;
for (i = 0; i < n; height[ranks[i++]] = k) for (k?k--:0, j = sa[ranks[i]-1]; r[i+k] = = R[j+k]; k++); } #define Root 1, N, 1 #define Lson L, M, RT << 1 #define Rson m + 1, R, RT << 1 |
1 int sum[n<<2],tot; void pushup (int rt) {Sum[rt] = min (sum[rt<<1],sum[rt<<1|1]),} void Build (int l,int R,int RT) {if (l
        = = r) {Sum[rt]=height[++tot];
    Return
    } int m = (l+r) >>1;
    Build (Lson);
    Build (Rson);
Pushup (RT); } int query (int l,int r,int rt,int ql,int qr) {if (l>qr| |
    QL&GT;R) return INF;
    if (L&GT;=QL&AMP;&AMP;R&LT;=QR) return SUM[RT];
int m = l+r>>1;    return min (Query (L,M,RT&LT;&LT;1,QL,QR), query (M+1,R,RT&LT;&LT;1|1,QL,QR));
    } int main () {int i,j,k,cas,t,t,x,y,p,q,z,now,pos;
    scanf ("%d", &t);
    cas=0;
        while (t--) {scanf ("%s", s);
        X=strlen (s); m=27;n=x;
        scanf ("%s", s+n);
        N=strlen (s); y=n-x;
        scanf ("%s", s+n);
        N=strlen (s); z=n-y-x;
        for (i=0;i<n;i++) seq[i]=s[i]-' a ' +1;
        seq[n]=0;
        Da (seq,n+1,m);
        Calheight (Seq,n);
        res=0;tot=0;
        Build (root);
        int sum=0;
        int vis[3]={0},tmp[n];
        I=j=1; while (i<=n| |
                J<=n) {if (sum<3&&j<=n) {now=i==j?1:query (root,i+1,j);
                P=sa[j],q=sa[j]+now-1;
                pos=3;
                if (p<x&&q<x) pos=0;
                else if (p<x+y&&p>=x&&q<x+y) Pos=1;
                else if (p>=x+y&&q<n) pos=2;
  Tmp[j]=pos;              j + +;
                if (pos==3) continue;
                if (!vis[pos]) sum++;
            vis[pos]++;
                } else if (sum==3&&i<=n) {Res=max (Res,query (root,i+1,j-1));
                vis[tmp[i]]--;
                if (!vis[tmp[i]]) sum--;
            i++;
        } else break;
    } printf ("Case%d:%d\n", ++cas,res);
} 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.