POJ3294---Life Forms(尾碼數組,二分+給尾碼分組),尾碼數組

來源:互聯網
上載者:User

POJ3294---Life Forms(尾碼數組,二分+給尾碼分組),尾碼數組

Description

You may have wondered why most extraterrestrial life forms resemble humans, differing by superficial traits such as height, colour, wrinkles, ears, eyebrows and the like. A few bear no human resemblance; these typically have geometric or amorphous shapes like cubes, oil slicks or clouds of dust.

The answer is given in the 146th episode of Star Trek - The Next Generation, titled The Chase. It turns out that in the vast majority of the quadrant’s life forms ended up with a large fragment of common DNA.

Given the DNA sequences of several life forms represented as strings of letters, you are to find the longest substring that is shared by more than half of them.

Input

Standard input contains several test cases. Each test case begins with 1 ≤ n ≤ 100, the number of life forms. n lines follow; each contains a string of lower case letters representing the DNA sequence of a life form. Each DNA sequence contains at least one and not more than 1000 letters. A line containing 0 follows the last test case.

Output

For each test case, output the longest string or strings shared by more than half of the life forms. If there are many, output all of them in alphabetical order. If there is no solution with at least one letter, output “?”. Leave an empty line between test cases.

Sample Input

3
abcdefg
bcdefgh
cdefghi
3
xxx
yyy
zzz
0

Sample Output

bcdefg
cdefgh

?

Source
Waterloo Local Contest, 2006.9.30

將串都連在一起,中間用沒有出現過的字元連起來,這些字元要不同,然後二分答案,給尾碼分組,看每組裡的尾碼是否出現在一半以上的串中

/*************************************************************************    > File Name: POJ3294.cpp    > Author: ALex    > Mail: zchao1995@gmail.com     > Created Time: 2015年04月03日 星期五 21時07分09秒 ************************************************************************/#include <functional>#include <algorithm>#include <iostream>#include <fstream>#include <cstring>#include <cstdio>#include <cmath>#include <cstdlib>#include <queue>#include <stack>#include <map>#include <bitset>#include <set>#include <vector>using namespace std;const double pi = acos(-1.0);const int inf = 0x3f3f3f3f;const double eps = 1e-15;typedef long long LL;typedef pair <int, int> PLL;int pos[110100];char str[1300];class SuffixArray{    public:        static const int N = 110100;        int init[N];        int X[N];        int Y[N];        int Rank[N];        int sa[N];        int height[N];        int buc[N];        bool vis[1200];        int size;        set <string> st;        void clear()        {            size = 0;        }        void insert(int n)        {            init[size++] = n;        }        bool cmp(int *r, int a, int b, int l)        {            return (r[a] == r[b] && r[a + l] == r[b + l]);        }        void getsa(int m = 256)        {            init[size] = 0;            int l, p, *x = X, *y = Y, n = size + 1;            for (int i = 0; i < m; ++i)            {                buc[i] = 0;            }            for (int i = 0; i < n; ++i)            {                ++buc[x[i] = init[i]];            }            for (int i = 1; i < m; ++i)            {                buc[i] += buc[i - 1];            }            for (int i = n - 1; i >= 0; --i)            {                sa[--buc[x[i]]] = i;            }            for (l = 1, p = 1; l <= n && p < n; m = p, l *= 2)            {                p = 0;                for (int i = n - l; i < n; ++i)                {                    y[p++] = i;                }                for (int i = 0; i < n; ++i)                {                    if (sa[i] >= l)                    {                        y[p++] = sa[i] - l;                    }                }                for (int i = 0; i < m; ++i)                {                    buc[i] = 0;                }                for (int i = 0; i < n; ++i)                {                    ++buc[x[y[i]]];                }                for (int i = 1; i < m; ++i)                {                    buc[i] += buc[i - 1];                }                for (int i = n - 1; i >= 0; --i)                {                    sa[--buc[x[y[i]]]] = y[i];                }                int i;                for (swap(x, y), x[sa[0]] = 0, p = 1, i = 1; i < n; ++i)                {                    x[sa[i]] = cmp(y, sa[i - 1], sa[i], l) ? p - 1 : p++;                }            }        }        void getheight()        {            int h = 0, n = size;            for (int i = 0; i <= n; ++i)            {                Rank[sa[i]] = i;            }            height[0] = 0;            for (int i = 0; i < n; ++i)            {                if (h > 0)                {                    --h;                }                int j = sa[Rank[i] - 1];                for (; i + h < n && j + h < n && init[i + h] == init[j + h]; ++h);                height[Rank[i] - 1] = h;            }        }        bool check(int k, int n)        {            int cnt = 1;            memset(vis, 0, sizeof(vis));            vis[pos[sa[1]]] = 1;            for (int i = 1; i < size; ++i)            {                if (height[i] >= k)                {                    if (pos[sa[i + 1]] != -1 && !vis[pos[sa[i + 1]]])                    {                        ++cnt;                        vis[pos[sa[i + 1]]] = 1;                    }                }                else                {                    if (cnt > n / 2)                    {                        return 1;                    }                    memset(vis, 0, sizeof(vis));                    cnt = 1;                    if (pos[sa[i + 1]] != -1)                    {                        vis[pos[sa[i + 1]]] = 1;                    }                }            }            return 0;        }        void solve(int n)        {            int l = 1, r = size, mid;            int ans = 0;            while (l <= r)            {                mid = (l + r) >> 1;                if (check(mid, n))                {                    ans = mid;                    l = mid + 1;                }                else                {                    r = mid - 1;                }            }            if (!ans)            {                printf("?\n");            }            else            {                   st.clear();                int cnt = 1;                memset(vis, 0, sizeof(vis));                vis[pos[sa[1]]] = 1;                for (int i = 0; i < size; ++i)                {                    if (height[i] >= ans)                    {                        if (!vis[pos[sa[i + 1]]])                        {                            ++cnt;                            vis[pos[sa[i + 1]]] = 1;                        }                        for (int j = sa[i + 1]; j < sa[i + 1] + ans; ++j)                        {                            str[j - sa[i + 1]] = (char)init[j];                        }                        str[ans] = '\0';                        st.insert(str);                    }                    else if (height[i] < ans)                    {                        if (cnt > n / 2)                        {                            set <string> :: iterator it;                            for (it = st.begin(); it != st.end(); ++it)                            {                                printf("%s\n", it -> c_str());                            }                        }                        st.clear();                        cnt = 1;                        memset(vis, 0, sizeof(vis));                        vis[pos[sa[i + 1]]] = 1;                    }                }            }        }}SA;int main(){    int n;    bool flag = 0;    while (~scanf("%d", &n), n)    {        int maxs = 0;        SA.clear();        int cnt = 0;        for (int i = 1; i <= n; ++i)        {            scanf("%s", str);            int len = strlen(str);            for (int j = 0; j < len; ++j)            {                SA.insert((int)str[j]);                maxs = max(maxs, (int)str[j]);                pos[cnt++] = i;            }            SA.insert((int)('z') + i);            pos[cnt++] = -1;        }        if (flag)        {            printf("\n");        }        else        {            flag = 1;        }        SA.getsa();        SA.getheight();        SA.solve(n);    }    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.