UVA 11107 Life Forms 后缀数组

白书上的例题,贴的模板。。。


Time Limit: 6666MS Memory Limit: Unknown 64bit IO Format: %lld & %llu

[]   [Go Back]   [Status]  

Description

Download as PDF

Problem C: Life Forms

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.

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.

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

Output for Sample Input

bcdefg
cdefgh

?

Gordon V. Cormack

[]   [Go Back]   [Status]  



#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>

using namespace std;

#define PB push_back
#define MP make_pair
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,l,h) for(int i=(l);i<=(h);++i)
#define DWN(i,h,l) for(int i=(h);i>=(l);--i)
#define CLR(vis) memset(vis,0,sizeof(vis))
#define MST(vis,pos) memset(vis,pos,sizeof(vis))
#define MAX3(a,b,c) max(a,max(b,c))
#define MAX4(a,b,c,d) max(max(a,b),max(c,d))
#define MIN3(a,b,c) min(a,min(b,c))
#define MIN4(a,b,c,d) min(min(a,b),min(c,d))
#define PI acos(-1.0)
#define INF 0x7FFFFFFF
#define LINF 1000000000000000000LL
#define eps 1e-8

typedef long long ll;

const int maxn=100000+1000;

int r[maxn] ;
int sa[maxn], height[maxn] ,rank[maxn] , id[maxn] ;
int wa[maxn] ,wb[maxn], ky[maxn], cn[maxn] ;
inline bool cmp(int *r ,int a,int b,int l){
    return r[a]==r[b] && r[a+l] == r[b+l] ;
}
void build_sa(int *r, int *sa ,int n , int m){
    int i, k , p , *x=wa ,*y = wb;
    for(i=0; i<m; i++) cn[i] = 0;
    for(i=0; i<n; i++) cn[ x[i] = r[i] ] ++ ;
    for(i=1; i<m; i++) cn[i] += cn[i-1] ;
    for(i=n-1; i>=0; i--) sa[ --cn[ x[i] ] ] = i ;
    for(k=1,p=1; p<n; k<<=1 , m=p) {
        for(p=0,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<n; i++) ky[i] = x[ y[i] ] ;
        for(i=0; i<m; i++) cn[i] = 0;
        for(i=0; i<n; i++) cn[ky[i]]++ ;
        for(i=1; i<m; i++) cn[i] += cn[i-1] ;
        for(i=n-1; i>=0; i--) sa[ --cn[ky[i]] ] = y[i] ;
        for(swap(x ,y),p=1,x[sa[0]]=0,i=1; i<n; i++)
        x[sa[i]] = cmp(y,sa[i-1],sa[i],k) ? p-1 : p++ ;
    }
    return ;
}
void get_height(int *s, int n) {
    int i, j , k=0;
    for(i=0; i<n; i++) rank[sa[i]] = i ;
    for(i=0; i<n-1; i++) {
        if(k) k-- ;
        j = sa[rank[i]-1] ;
        while(s[i+k] == s[j+k]) k++ ;
        height[rank[i]] = k;
    }
}


bool check(int n ,int m, int mid)
{
    set<int>vis ;
    vis.insert(id[sa[1]]) ;
    for(int i=2;i<n;i++)
    {
        while(i<n && height[i] >= mid)
          vis.insert(id[sa[i]]),i++ ;
        if(vis.size()*2>m)
           return true ;
        vis.clear();
        vis.insert(id[sa[i]]) ;
    }
    return false ;
}


void print(int n,int m,int p)
{
    set<int>vis ;
    vis.insert(id[sa[1]]);
    for(int i=2; i<n; i++) {
        while(i<n && height[i]>= p)
            vis.insert(id[sa[i]]),i++ ;
        if(vis.size()*2>m)
        {
            int a=sa[i-1] ;
            for(int j=0;j<p;j++)
               putchar(r[a+j]) ;
            puts("") ;
        }
        vis.clear() ;
        vis.insert(id[sa[i]]) ;
    }
}


int main()
{
    int m,flag=1;
    while(scanf("%d",&m)==1 && m)
    {
        if(!flag) puts("");
        else     flag=0;
        char str[1111];
        int n=0;
        FOR(i,1,m)
        {
            CLR(str);
            scanf("%s",str);
            for(int j=0; str[j]; ++j)
            {
                id[n]=i;
                r[n++]=str[j];
            }
            id[n]=i;
            r[n++]='z'+i;
        }
        if(m==1)
        {
            puts(str);
            continue;
        }
        id[n]=n+1;
        r[n++]=0;

        build_sa(r,sa,n,'z'+m+2);
        get_height(r,n);

        if(!check(n,m,1))
        {
            puts("?");
            continue;
        }

        int L=1,R=1000,mid;
        while(L<R)
        {
            mid=L+(R-L+1)/2;
            if(check(n,m,mid))
                L=mid ;
            else
                R=mid-1;
        }

        print(n,m,L);

    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值