Life Forms
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 15422 Accepted: 4537
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
/*
后缀数组+二分答案.
唉论文坑人呐
题目描述根本不一样.
多串匹配,用不同的字符连接起来即可.
若反转也算的话就将字符串反转接上.
*/
#include<iostream>
#include<cstring>
#include<cstdio>
#define MAXN 110000
using namespace std;
int n,K,m=231,total,ans,belong[MAXN],pos[MAXN],p,tot,tmp[MAXN],sa[MAXN],rank1[MAXN],c[MAXN],ht[MAXN],t1[MAXN],t2[MAXN],s[MAXN];
char ch[MAXN];
bool vis[MAXN],f[MAXN];
bool cmp(int *y,int a,int b,int k)
{
int a1=y[a],b1=y[b];
int a2=a+k>=n?-1:y[a+k];
int b2=b+k>=n?-1:y[b+k];
return a1==b1&&a2==b2;
}
void slovesa()
{
int *x=t1,*y=t2;
for(int i=0;i<m;i++) c[i]=0;
for(int i=0;i<n;i++) c[x[i]=s[i]]++;
for(int i=1;i<m;i++) c[i]+=c[i-1];
for(int i=n-1;i>=0;i--) sa[--c[x[i]]]=i;
for(int k=1,p=0;k<=n;k<<=1,m=p,p=0)
{
for(int i=n-k;i<n;i++) y[p++]=i;
for(int i=0;i<n;i++) if(sa[i]>=k) y[p++]=sa[i]-k;
for(int i=0;i<m;i++) c[i]=0;
for(int i=0;i<n;i++) c[x[y[i]]]++;
for(int i=1;i<m;i++) c[i]+=c[i-1];
for(int i=n-1;i>=0;i--) sa[--c[x[y[i]]]]=y[i];
swap(x,y),p=1,x[sa[0]]=0;
for(int i=1;i<n;i++)
{
if(cmp(y,sa[i-1],sa[i],k)) x[sa[i]]=p-1;
else x[sa[i]]=p++;
}
if(p>=n) break;
}
}
void sloveheight()
{
int k=0;
for(int i=0;i<n;i++) rank1[sa[i]]=i;
for(int i=0;i<n;ht[rank1[i++]]=k)
{
int j=sa[rank1[i]-1];
if(k) k--;
while(j+k<n&&i+k<n&&s[i+k]==s[j+k]) k++;
}
ht[0]=0;
}
bool check(int x)
{
bool ansflag=false;int tmpp=0,flag=0,ok=0;
for(int i=1;i<n;i++)
{
if(ht[i]>=x)
{
vis[belong[sa[i-1]]]=true;
vis[belong[sa[i]]]=true;
if(f[sa[i]]) ok=sa[i];
}
else
{
for(int j=1;j<=tot;j++)
{
if(vis[j]) flag++;
vis[j]=false;
}
if(flag>tot/2)
{
ansflag=true;
if(ok) tmp[++tmpp]=ok;
}
flag=0;
}
}
flag=0;
for(int j=1;j<=tot;j++)
{
if(vis[j]) flag++;
vis[j]=false;
}
if(flag>tot/2)
{
ansflag=true;
if(f[sa[n-1]]) tmp[++tmpp]=sa[n-1];
}
if(ansflag)
{
for(int i=1;i<=tmpp;i++) pos[i]=tmp[i];
p=tmpp;return true;
}
return false;
}
void erfen(int l,int r)
{
int mid;
while(l<=r)
{
mid=(l+r)>>1;
if(check(mid)) ans=mid,l=mid+1;
else r=mid-1;
}
}
void Clear()
{
total=0,p=0,ans=0,m=231;
memset(sa,0,sizeof sa);
}
void slove()
{
if(!ans||!p) printf("?
");
else for(int i=1;i<=p;i++)
{
for(int j=pos[i];j<=pos[i]+ans-1;j++)
printf("%c",char(s[j]));
printf("
");
}
printf("
");
}
int main()
{
while(scanf("%d",&tot))
{
if(!tot) break;Clear();
for(int i=1;i<=tot;i++)
{
scanf("%s",ch);
for(int j=0;j<strlen(ch);j++) s[total]=ch[j],belong[total]=i,f[total++]=true;
if(i!=tot) s[total++]=130+i;
//for(int j=strlen(ch)-1;j>=0;j--) s[total]=ch[j],belong[total]=i,f[total++]=false;
//if(i!=tot) s[total++]=130+i;
}
s[total++]=0;n=total;
slovesa(),sloveheight(),erfen(1,n/tot+1),slove();
}
return 0;
}