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\n",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;
}
时间: 2024-10-09 16:23:39

Life Forms的相关文章

Catch Application Exceptions in a Windows Forms Application

You need to handle the System.Windows.Forms.Application.ThreadException event for Windows Forms. This article really helped me: http://bytes.com/forum/thread236199.html. Application.ThreadException += new ThreadExceptionEventHandler(MyCommonException

菜鸟的Xamarin.Forms前行之路——原生Toast的简单实现方法

项目中信息提示框,貌似只有个DisplayAlert,信息提示太过于单一,且在有些场合Toast更加实用,以下是一个简单的原生Toast的实现方法 项目地址:https://github.com/weiweu/TestProject/tree/dev/Toast 共享项目 定义一个接口IToast,包括Short和Long两个方法: public interface IToast { void LongAlert(string message); void ShortAlert(string m

张高兴的 Xamarin.Forms 开发笔记:为 Android 与 iOS 引入 UWP 风格的汉堡菜单 ( MasterDetailPage )

所谓 UWP 样式的汉堡菜单,我曾在"张高兴的 UWP 开发笔记:汉堡菜单进阶"里说过,也就是使用 Segoe MDL2 Assets 字体作为左侧 Icon,并且左侧使用填充颜色的矩形用来表示 ListView 的选中.如下图 但怎样通过 Xamarin.Forms ,将这一样式的汉堡菜单带入到 Android 与 iOS 中呢? 一.大纲-细节模式简介 讲代码前首先来说说这种导航模式,官方称"大纲-细节模式"(MasterDetail).左侧的汉堡菜单称为&qu

菜鸟的Xamarin.Forms前行之路——绪言

作者入门时间不是很久,差不多一年,期间自学的东西比较杂乱,到目前为止,编程方面的知识比较薄弱.之所以做这个系列,也只是因为做了两个月的Xamarin.Forms方面的东西,由于资料和自身实力的原因,过程走的比较艰难,但所幸的是也解决了部分的问题,积累了一些经验.期望通过这个系列,和大家分享经验,查漏纠错. 作为一个菜鸟,在解决问题的时候,往往比较直接,就是仅仅为了解决问题,期间可能根本没有考虑性能等方面的问题.所以在这个系列中,问题肯定是作者亲身实践能够解决的,但是在性能资源等方面作者没有做过考

Xamarin.Forms开发APP

Xamarin.Forms+Prism(1)-- 开发准备 准备: 1.VS2017(推荐)或VS2015: 2.JDK 1.8以上: 3.Xamarin.Forms 最新版: 4.Prism 扩展,打开VS的扩展和更新,在联机中,搜索Prism,安装第一个扩展Prism Template Pack: 5.Android SDK,这个下载已经非常快了,国内已经支持Android环境下载. 6.都准备好后,请确保创建一个新的Xamarin.Forms程序后,能正常调试运行,不能调试运行的,请百度或

asp.net权限认证:Forms认证

摘要: 明天就除夕了,闲着也是闲着,特地总结一些关于.net下的权限认证的方法. 一.Forms认证示意图 Forms认证即是表单认证,需提供身份id和密码password的进行认证和授权管理. 应该是大家比较熟悉的一种,刚接触.net可能都会学学这个东西. 下面看看他的工作方式: 二.看图太乏味,我准备了一个demo 因为默认首页为:IndexController/Index,这个页面只要一行字 “Index”, 效果图: OK,页面没有做任何权限控制,显示正常. 接下来看看DefaultCo

DotNetBar for Windows Forms 14.0.0.3_冰河之刃重打包版原创发布

关于 DotNetBar for Windows Forms 14.0.0.3_冰河之刃重打包版 --------------------11.8.0.8_冰河之刃重打包版---------------------------------------------------------基于 官方原版的安装包 + http://www.cnblogs.com/tracky 提供的补丁DLL制作而成.安装之后,直接就可以用了.省心省事.不必再单独的打一次补丁包了.本安装包和补丁包一样都删除了官方自带

Displaying Window In Center In Oracle Forms 6i

Center window automatically  in Oracle Forms 6i, use the following procedure by passing window name as parameter: Example PROCEDURE auto_centre (pwn in varchar2) ISvw number := get_window_property(forms_mdi_window, width);vh number := get_window_prop

Using GET_APPLICATION_PROPERTY in Oracle D2k Forms

Using GET_APPLICATION_PROPERTY in Oracle D2k Forms DescriptionReturns information about the current Form Builder application. You must call the built-in once for eachvalue you want to retrieve.Usage NotesTo request a complete login, including an appe

Writing On-Error Trigger In Oracle Forms

Suppose you want to handle an error in oracle forms and want to display custom error message for that error, but also you want to customize more for a particular error. For example there are many fields in form with required property is set to TRUE f