POJ 2774 Long Long Message (longest common substring-beginner suffix array)

Source: Internet
Author: User

Two divine papers on suffix arrays:


Proceedings of the National Training Team 2004 Xu Zhilei
the "suffix array"-a powerful tool for processing strings-in algorithm collection

a lot of people's templates are written on paper.

include kuangbin Template: (da algorithm)more difficult to understand in the template is two points 1. Sort by keywords 2. Increase the string length by one bit
Sort by keywords in fact, the Cardinal sort is quite the two-digit sort when the first row, and then row 10 bitIt's the same here. 2^k length string, and then the string of 2^k length before the end of the 2^ (k+1) character length suffix array sa
adds a string to the number of points, in order to let the meaningful string rank from 1, but also easy to use in the back without a specific cross-border and other details
/**suffix array* multiplication Algorithm O (N*LOGN) * The array length to be sorted is N, put in 0~n-1, fill a 0*da (str, n+1,sa,rank,height,) on the last side, or//note is n+1;* for example: *n = 8;*num[] = {1, 1, 2, 1, 1, 1, 1, 2, $}; Note Num Last is 0, others greater than 0*rank[] = {4, 6, 8, 1, 2, 3, 5, 7, 0};rank[0~n-1] are valid values, Rank[n] must be 0 invalid (key *sa[] = {8, 3, 4, 5, 0, 6, 1, 7, 2};sa[1~n] is a valid value, Sa[0] must be n is an invalid value (key) *height[]= {0, 0, 3, 2, 3, 1, 2, 0, 1};height[2~n] For valid values **/const int maxn=20010;int t1[maxn],t2[maxn],c[maxn];//The intermediate variable required for the SA array, no assignment required//the string to be sorted in S array, from s[0] to s[n-1], length n, And the maximum value is less than m,//except s[n-1] all S[i] are greater than the 0,r[n-1]=0//function ends after the result is placed in the SA array bool cmp (int *r,int a,int b,int l) {return r[a] = = R[b] &&am P R[a+l] = = R[b+l];}    void da (int str[],int sa[],int rank[],int height[],int n,int m) {n++;//Note int i, J, p, *x = t1, *y = t2;    The first round of cardinality sorting, if the maximum value of S is large, can be changed to quick sort (change only the first round) for (i = 0;i < m;i++) C[i] = 0;    for (i = 0;i < n;i++) C[x[i] = str[i]]++;    for (i = 1;i < m;i++) C[i] + = c[i-1];    for (i = n-1;i >= 0;i--) sa[--c[x[i]] = i; for (j = 1;j <= N; J <<= 1) {p = 0;       Directly using the SA array to sort the second keyword for (i = n-j; i < n; i++) y[p++] = i;//The second keyword is null for the smallest for (i = 0; i < n; i++)        if (Sa[i] >= j) y[p++] = sa[i]-J;        So the array y is saved by the second keyword sorted by the result//radix sort the first keyword for (i = 0; i < m; i++) c[i] = 0;        for (i = 0; i < n; i++) c[x[y[i]]]++;        for (i = 1; i < m;i++) C[i] + = c[i-1];        for (i = n-1; I >= 0;i--) sa[--c[x[y[i]]] = y[i]; Calculates a new x array of swaps based on the SA and X arrays, <span style= "white-space:pre" ></span>//small optimization p = 1;        X[sa[0]] = 0;        for (i = 1;i < n;i++) X[sa[i] = CMP (y,sa[i-1],sa[i],j)? p-1:p++;    if (P >= N) break;<span style= "White-space:pre" ></span>//small optimization m = p;//the next cardinal order maximum value} int k = 0;    n--;//Note for (i = 0;i <= n;i++) rank[sa[i]] = i;        for (i = 0;i < n;i++) {if (k) k--;        j = Sa[rank[i]-1];        while (str[i+k] = = Str[j+k]) k++;    Height[rank[i]] = k; }}int Rank[maxn],height[maxn];int rmq[maxn];int mM[maxn];int best[20][maxn];void initrmq (int n) {mm[0]=-1; for (int i=1;i<=n;i++) mm[i]= ((i& (i-1)) ==0)?    MM[I-1]+1:MM[I-1];    for (int i=1;i<=n;i++) best[0][i]=i;            for (int i=1;i<=mm[n];i++) for (int j=1;j+ (1<<i) -1<=n;j++) {int a=best[i-1][j];            int b=best[i-1][j+ (1<< (i-1))];            if (rmq[a]<rmq[b]) best[i][j]=a;        else best[i][j]=b;    }}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) {a=rank[a];b=rank[b];    if (a>b) swap (A, b); Return HEIGHT[ASKRMQ (A+1,b)];}        Char str[maxn];int r[maxn];int sa[maxn];int Main () {while (scanf ("%s", str) = = 1) {int len = strlen (str);        int n = 2*len + 1;        for (int i = 0;i < len;i++) R[i] = Str[i];        for (int i = 0;i < len;i++) R[len + 1 + i] = str[len-1-i];        R[len] = 1;        R[n] = 0; Da (r,sa,rank,heIGHT,N,128);        for (int i=1;i<=n;i++) rmq[i]=height[i];        INITRMQ (n);        int ans=0,st;        int tmp;                for (int i=0;i<len;i++) {TMP=LCP (i,n-i);//even symmetric if (2*tmp>ans) {                ans=2*tmp;            st=i-tmp;                } TMP=LCP (i,n-i-1);//odd symmetric if (2*tmp-1>ans) {ans=2*tmp-1;            st=i-tmp+1;        }} str[st+ans]=0;    printf ("%s\n", str+st); } return 0;}

Don't want to learn DC3, the template feel more difficult to understand
for the two-string LCP, put two strings and a string, the middle plus the non-appearing characters (excluding LCP across the case of two strings), and then the suffix array of height array. The start character of the two substring is then excluded from the same string.
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm>using namespace std;const int MAXN = 200000+100;int t1[maxn],t2[maxn],c[maxn];bool cmp (int *r,int a,int b,int l) {return r[a] = = R[b] & amp;& R[a+l] = = R[b+l];}    void da (int str[],int sa[],int rank[],int height[],int n,int m) {n++;    int I, J, p, *x = t1, *y = t2;    First round Cardinal Sort, if s maximum value is large, can be changed to quick sort for (i = 0;i < m;i++) C[i] = 0;    for (i = 0;i < n;i++) C[x[i] = str[i]]++;    for (i = 1;i < m;i++) C[i] + = c[i-1];    for (i = n-1;i >= 0;i--) sa[--c[x[i]] = i;        for (j = 1;j <= N; J <<= 1) {p = 0; Directly using the SA array to sort the second keyword for (i = n-j; i < n; i++) y[p++] = i;//The second key is empty for the minimum for (i = 0; i < n; i++) if (sa[        I] >= j) y[p++] = sa[i]-J;        So the array y is saved by the second keyword sorted by the result//radix sort the first keyword for (i = 0; i < m; i++) c[i] = 0;        for (i = 0; i < n; i++) c[x[y[i]]]++;        for (i = 1; i < m;i++) C[i] + = c[i-1]; for (i = n-1;        I >= 0;i--) sa[--c[x[y[i]] [] = y[i];        Calculates a new x array of swaps based on the SA and X arrays; p = 1;        X[sa[0]] = 0;        for (i = 1;i < n;i++) X[sa[i] = CMP (y,sa[i-1],sa[i],j)? p-1:p++;        if (P >= N) break;    m = p;//The maximum value of the next cardinal order} int k = 0;    n--;    for (i = 0;i <= n;i++) rank[sa[i]] = i;        for (i = 0;i < n;i++) {if (k) k--;        j = Sa[rank[i]-1];        while (str[i+k] = = Str[j+k]) k++;    Height[rank[i]] = k;    }}int Rank[maxn],height[maxn];char str[maxn];int r[maxn];int sa[maxn];int main () {int len1,len2;    scanf ("%s", str);    Len1=strlen (str);    for (int i=0;i<len1;i++) r[i]=str[i]-' a ' +2;    R[len1]=1;    scanf ("%s", str);    Len2=strlen (str);    for (int i=0;i<len2;i++) r[len1+1+i]=str[i]-' a ' +2;    int n=len1+len2+1;    r[n]=0;    Da (r,sa,rank,height,n,30);    int ans = 0; for (int i = 2; i < n; i + +) if ((Sa[i] < len1 && Sa[i-1] > len1) | |        (Sa[i-1] < len1 && Sa[i] > Len1){ans = max (ans, height[i]);    } printf ("%d\n", ans); return 0;}




POJ 2774 Long Long Message (longest common substring-beginner suffix array)

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.