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;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值