zoukankan      html  css  js  c++  java
  • Life Forms

    Life Forms
    Time Limit: 5000MS   Memory Limit: 65536K
         

    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
    
    ?
    分析:后缀数组+二分;
       注意数组要开大,考虑到中间字符;
       另外最好全部转换成数字,否则插入中间字符容易RE;
    代码:
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <algorithm>
    #include <climits>
    #include <cstring>
    #include <string>
    #include <set>
    #include <map>
    #include <queue>
    #include <stack>
    #include <vector>
    #include <list>
    #define rep(i,m,n) for(i=m;i<=n;i++)
    #define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
    #define mod 1000000007
    #define inf 0x3f3f3f3f
    #define vi vector<int>
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define ll long long
    #define ld long double
    #define pi acos(-1.0)
    #define pii pair<int,int>
    #define Lson L, mid, ls[rt]
    #define Rson mid+1, R, rs[rt]
    #define sys system("pause")
    #define freopen freopen("in.txt","r",stdin)
    const int maxn=2e5+10;
    using namespace std;
    ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
    ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
    inline ll read()
    {
        ll x=0;int f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    int n,m,k,t,pos[maxn],flag[maxn],cntA[maxn],cntB[maxn],sa[maxn],lev[maxn],height[maxn],A[maxn],B[maxn],tsa[maxn],cnt,ch[maxn];
    char a[maxn],ch1[maxn];
    bool ok;
    set<string>ret;
    void solve()
    {
        for (int i = 0; i < 300; i ++) cntA[i] = 0;
        for (int i = 1; i <= n; i ++) cntA[ch[i]] ++;
        for (int i = 1; i < 300; i ++) cntA[i] += cntA[i - 1];
        for (int i = n; i; i --) sa[cntA[ch[i]] --] = i;
        lev[sa[1]] = 1;
        for (int i = 2; i <= n; i ++)
        {
            lev[sa[i]] = lev[sa[i - 1]];
            if (ch[sa[i]] != ch[sa[i - 1]]) lev[sa[i]] ++;
        }
        for (int l = 1; lev[sa[n]] < n; l <<= 1)
        {
            for (int i = 0; i <= n; i ++) cntA[i] = 0;
            for (int i = 0; i <= n; i ++) cntB[i] = 0;
            for (int i = 1; i <= n; i ++)
            {
                cntA[A[i] = lev[i]] ++;
                cntB[B[i] = (i + l <= n) ? lev[i + l] : 0] ++;
            }
            for (int i = 1; i <= n; i ++) cntB[i] += cntB[i - 1];
            for (int i = n; i; i --) tsa[cntB[B[i]] --] = i;
            for (int i = 1; i <= n; i ++) cntA[i] += cntA[i - 1];
            for (int i = n; i; i --) sa[cntA[A[tsa[i]]] --] = tsa[i];
            lev[sa[1]] = 1;
            for (int i = 2; i <= n; i ++)
            {
                lev[sa[i]] = lev[sa[i - 1]];
                if (A[sa[i]] != A[sa[i - 1]] || B[sa[i]] != B[sa[i - 1]]) lev[sa[i]] ++;
            }
        }
        for (int i = 1, j = 0; i <= n; i ++)
        {
            if (j) j --;
            while (ch[i + j] == ch[sa[lev[i] - 1] + j]) j ++;
            height[lev[i]] = j;
        }
    }
    bool check(int p)
    {
        int tmp=0;
        a[p]=0;
        bool now_flag=false;
        for(int i=1;i<=n+1;i++)
        {
            if(height[i]>=p)
            {
                if(!tmp)
                {
                    int b=lower_bound(pos+1,pos+m+1,sa[i-1])-pos;
                    if(b==m+1||pos[b]>sa[i-1])b--;
                    if(flag[b]!=cnt)flag[b]=cnt,tmp++;
                    strncpy(a,ch1+sa[i],p);
                }
                int b=lower_bound(pos+1,pos+m+1,sa[i])-pos;
                if(b==m+1||pos[b]>sa[i])b--;
                if(flag[b]!=cnt)flag[b]=cnt,tmp++;
            }
            else
            {
                if(tmp>m/2)
                {
                    if(!now_flag)ret.clear();
                    ret.insert(a);
                    now_flag=true;
                }
                cnt++,tmp=0;
            }
        }
        return now_flag;
    }
    int main()
    {
        int i,j;
        while(~scanf("%d",&m)&&m)
        {
            if(ok)puts("");
            else ok=true;
            memset(flag,0,sizeof(flag));
            ret.clear();
            n=0;
            rep(i,1,m)
            {
                scanf("%s",a);
                int len=strlen(a);
                if(i!=1)strcat(ch1+1,a);
                else strcpy(ch1+1,a);
                for(j=n+1;j<=n+len;j++)ch[j]=(a[j-n-1]-'a');
                char str[3];
                str[0]='Z';
                str[1]=0;
                ch[n+len+1]=25+i;
                strcat(ch1+1,str);
                pos[i]=n+1;
                n+=len+1;
            }
            if(m==1){printf("%s
    ",a);continue;}
            height[n+1]=0;
            solve();
            cnt=0;
            int l=1,r=1000,tmp=0;
            while(l<=r)
            {
                int mid=l+r>>1;
                if(check(mid))tmp=mid,l=mid+1;
                else r=mid-1;
            }
            if(!tmp)puts("?");
            else
            {
                for(string x:ret)cout<<x<<endl;
            }
        }
        //system("Pause");
        return 0;
    }
  • 相关阅读:
    Tomcat 三种运行方式
    MariaDB介绍
    Nginx 平滑升级
    代理命令 proxy_pass 详解
    Nginx 和 Tomcat 负载均衡
    基于Apache和tomcat实现负载均衡
    centos7 通过源码编译的方式安装和配置Apache
    基于nginx结合openssl实现https
    HTTP 和 HTTPS 区别
    linux系统中修改别名配置文件,构建命令别名
  • 原文地址:https://www.cnblogs.com/dyzll/p/6009658.html
Copyright © 2011-2022 走看看