UVA 11107 life Forms (suffix array + dichotomy)

Source: Internet
Author: User

Ideas:

Suffix array + two points:

Let's first concatenate the N strings with different characters, and note that the last one is to be prefixed with a character.

It then asks for the suffix array of the string.

Then the two-point answer, judging the length len is feasible. The Judgment method is the same routine, the SA array is chunked, each piece is greater than or equal to the length of Len, and then determine if each piece has more than half of the string in this piece, you can open a flag array, record each piece of string, and finally statistics flag array can be.

So we got the longest length of the two cents Len.

Then print the solution.

The same way to enumerate each piece, if this piece is legal, because the first Len character of each string is the same, the direct fetch of the first Len character is the answer.

The final sort can be.

#include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <string

> #define SIZ (x) (int) x.size () using namespace std;

const int MAXN = 1000000 + 10;

int T1[MAXN], T2[MAXN], C[MAXN];

BOOL CMP (int* r, int a,int b,int l) {return r[a] = = R[b] && r[a+l] = = R[b+l];}
    void da (int str[], int sa[], int rank[], int lcp[], int n, int m) {++n;
    int I, J, p, *x = t1, *y = t2;
for (i = 0; i < m; ++i) c[i] = 0;
    Puts ("HHA");
    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;
        for (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 < 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];
        Swap (x, y); 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;
    } 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;
    Lcp[rank[i]] = k;
}} int LCP[MAXN], A[MAXN], SA[MAXN], RANK[MAXN];

int n,k;


Char s[1000 + 10];
vector<int>bit;
int cnt, ID;
int flag[100 + 10];
int nn;
    BOOL Judge (int m) {int cp = N/2;
    Bit.clear ();
    Bit.push_back (sa[1]);
        for (int i = 2; I <= cnt; ++i) {if (Lcp[i] >= m) bit.push_back (Sa[i]);
            else {memset (flag,0,sizeof flag);
                for (int j = 0; J < Siz (bit); ++j) {int v = bit[j]/(NN+1);
            FLAG[V] = 1;} int sum = 0;
            for (int j = 0; J < n; ++j) sum + = Flag[j];
            if (Sum > CP) return true;
            Bit.clear ();
        Bit.push_back (Sa[i]);
    }} memset (Flag,0,sizeof flag);
        for (int j = 0; J < Siz (bit); ++j) {int v = bit[j]/(NN+1);
    FLAG[V] = 1;
    } int sum = 0;
    for (int j = 0; J < n; ++j) sum + = Flag[j];
    if (Sum > CP) return true;
return false;
} int ks = 0;

vector<string>vec;
        void solve (int len) {if (len = = 0) {puts ("?");
    Return
    } int cp = N/2;
    Bit.clear ();
    Bit.push_back (sa[1]);
        for (int i = 2; I <= cnt; ++i) {if (Lcp[i] >= len) bit.push_back (Sa[i]);
            else {string tmp;


            for (int j = bit[0], k= 0, K < Len; ++k, ++j) {tmp + = A[j]-1 + ' a ';
            } int kk = Siz (bit);
            memset (flag,0,sizeof flag); for (int j = 0; J &LT Siz (bit);
                ++J) {int v = bit[j]/(NN+1);
            FLAG[V] = 1;
            } int sum = 0;
            for (int j = 0; J < n; ++j) sum + = Flag[j];
                if (Sum > CP) {string ans;
                    for (int i = bit[0], j = 0; j < Len; ++j,++i) {char ch = a[i]-1+ ' a ';
                Ans + = ch;
            } vec.push_back (ANS);
            } bit.clear ();
        Bit.push_back (Sa[i]);
    }} memset (Flag,0,sizeof flag);
        for (int j = 0; J < Siz (bit); ++j) {int v = bit[j]/(NN+1);
    FLAG[V] = 1;
    } int sum = 0;
    for (int j = 0; J < n; ++j) sum + = Flag[j];
        if (Sum > CP) {string ans;
            for (int i = bit[0], j = 0; j < Len; ++j,++i) {char ch = a[i]-1+ ' a ';
        Ans + = ch;
    } vec.push_back (ANS);
   }} int main () {while (~SCANF ("%d", &n) && N) {     Vec.clear ();
        id=26, cnt = 0;
        int Len;
            for (int i = 0; i < n; ++i) {scanf ("%s", s);
            if (i = = 0) nn = strlen (s);
            for (int j = 0; s[j]; ++j) {a[cnt++] = s[j]-' a ' + 1;
        } a[cnt++] = ++id;
        } a[cnt] = 0;
        Da (A, SA, Rank, LCP, CNT, 200);
        int L = 1, r = cnt, m;
            while (L <= r) {m = L + R >> 1;
            if (judge (m)) L = m + 1;
        else R = m-1;
        } if (ks++) Putchar (' \ n ');
        Solve (R);
        Sort (Vec.begin (), Vec.end ());
        for (int i = 0; i < Siz (VEC); ++i) {printf ("%s\n", Vec[i].c_str ());
}} 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.