poj 3294 Life Forms(不小于k 个字符串中的最长子串)


Life Forms
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 9941   Accepted: 2740

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

?
 
  
题意:给定n 个字符串,求出现在不小于k 个字符串中的最长子串。
思路:将n个字符串连起来,中间用不相同的且没有出现在字符串中的字符隔开,求后缀数组。然后二分答案,将后缀分成若干组,判断每组的后缀是否出现在不小于k 个的原串中。这个做法的时间复杂度为O(nlogn)。
 
  
AC代码:
 
  
#include 
#include 
#include 
#include 
#include 
#include 
#define ll long long
using namespace std;

const int maxn = 200005;
const int INF = 1e9;

int s[maxn];
int sa[maxn], t[maxn], t2[maxn], c[maxn], n, mm;
int rank[maxn], height[maxn];
void build_sa(int n, int m){
    int i, *x = t, *y = t2;
    for(i = 0; i < m; i++) c[i] = 0;
    for(i = 0; i < n; i++) c[x[i] = s[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(int k = 1; k <= n; k <<= 1)
    {
        int p = 0;
        for(i = n - k; i < n; i++) y[p++] = i;
        for(i = 0; i < n; i++) if(sa[i] >= k) y[p++] = sa[i] - k;
        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]] = y[sa[i - 1]] == y[sa[i]] && y[sa[i - 1] + k] == y[sa[i] + k] ? p - 1 : p++;
        if(p >= n) break;
        m = p;
    }
}
void getHeight(){
    int i, j, k = 0;
    for(i = 1; i <= n; i++) rank[sa[i]] = i;
    for(i = 0; i < n; i++)
    {
        if(k) k--;
        j = sa[rank[i] - 1];
        while(s[i + k] == s[j + k]) k++;
        height[rank[i]] = k;
    }
}
int rmq[maxn][20];
void initRMQ(){
    for(int i = 1; i <= n; i++) rmq[i][0] = rmq[i][0] = height[i];
    for(int k = 1; (1 << k) <= n; k++)
    for(int i = 1; i + (1 << k) - 1 <= n; i++)
    {
        rmq[i][k]=min(rmq[i][k-1],rmq[i+(1<<(k-1))][k-1]);
    }
}
int lcp(int a,int b)
{
    a = rank[a], b = rank[b];
    if(a > b) swap(a, b);
    a++;
    int k = log(b - a + 1.0) / log(2.0);
    return min(rmq[a][k], rmq[b - (1 << k) + 1][k]);
}
char str[105][1005];
int id[maxn];
bool ok(int d, bool out){
    bool vis[105];
    memset(vis, false, sizeof(vis));
    int cnt = 0;
    for(int i = 1; i <= n; i++)
    {
        if(height[i] < d)
        {
            if(cnt > mm / 2 && out)
            {
                for(int j = 0, k = sa[i - 1]; j < d; j++) printf("%c", s[k + j] + 'a' - 1);
                puts("");
            }
            cnt = 0;
            memset(vis, false, sizeof(vis));
        }
        else
        {
            if(!vis[id[sa[i - 1]]])
            {
                vis[id[sa[i - 1]]] = true;
                cnt++;
            }
            if(!vis[id[sa[i]]])
            {
                vis[id[sa[i]]] = true;
                cnt++;
            }
            if(!out && cnt > mm / 2) return true;
        }
    }
    return false;
}
int main()
{
    bool flag = 0;
    while(scanf("%d", &mm), mm)
    {
        if(flag) puts("");
        flag = 1;
        for(int i = 0; i < mm; i++)
        scanf("%s", str[i]);
        n = 0;
        for(int i = 0 ; i < mm; i++)
        {
            for(int j = 0; str[i][j]; j++)
            {
                int ch = str[i][j] - 'a' + 1;
                s[n] = ch;
                id[n++] = i;
            }
            s[n] = 27 + i;
            id[n++] = i;
        }
        n--;
        s[n] = 0;
        build_sa(n + 1, 256);
        getHeight();
        int low = 0, high = 1000, ans = -1;
        while(low <= high)
        {
            int mid = (low + high) >> 1;
            if(ok(mid, 0))
            {
                ans = mid;
                low = mid + 1;
            }
            else high = mid - 1;
        }
        if(ans <= 0) printf("?\n");
        else ok(ans, 1);
    }
    return 0;
}

你可能感兴趣的:(poj,后缀数组)