poj3294 --Life Forms

时间:2023-03-09 22:11:34
poj3294 --Life Forms
Life Forms
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 12483   Accepted: 3501

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 ? 瘠薄
 #include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<iostream>
#define maxn 200005
int ws[maxn],wa[maxn],sa[maxn],num[maxn],n,wv[maxn],rank[maxn];
int h[maxn],wb[maxn],sum[maxn],m;
char str[][];
bool cmp(int *r,int a,int b,int l){
return r[a]==r[b]&&r[a+l]==r[b+l];
} void da(int *r,int *sa,int n,int m){
int *t,*x=wa,*y=wb,i,j,p;
for (i=;i<m;i++) ws[i]=;
for (i=;i<n;i++) x[i]=r[i];
for (i=;i<n;i++) ws[x[i]]++;
for (i=;i<m;i++) ws[i]+=ws[i-];
for (i=n-;i>=;i--) sa[--ws[x[i]]]=i;
for (j=,p=;p<n;j*=,m=p){
for (p=,i=n-j;i<n;i++) y[p++]=i;
for (i=;i<n;i++) if (sa[i]-j>=) y[p++]=sa[i]-j;
for (i=;i<m;i++) ws[i]=;
for (i=;i<n;i++) wv[i]=x[y[i]];
for (i=;i<n;i++) ws[wv[i]]++;
for (i=;i<m;i++) ws[i]+=ws[i-];
for (i=n-;i>=;i--) sa[--ws[wv[i]]]=y[i];
for (t=x,x=y,y=t,i=,p=,x[sa[]]=;i<n;i++)
x[sa[i]]=cmp(y,sa[i],sa[i-],j)?p-:p++;
}
}
void cal(int *r,int n){
int i,j,k=;
for (int i=;i<=n;i++) rank[sa[i]]=i;
for (int i=;i<n;h[rank[i++]]=k)
for (k?k--:,j=sa[rank[i]-];r[i+k]==r[j+k];k++);
}
int getid(int k){
int l=,r=n-,mid;
while (l<r){
mid=(l+r)/;
if (sum[mid]<k) {
l=mid+;
}
else
r=mid;
}
return l;
} bool check(int len,int out=){
int i=n+,j,k,id,cnt;
bool f[maxn];
while (){
while (i<=m&&h[i]<len) i++;
if (i>m) break;
memset(f,,sizeof f);
id=getid(sa[i-]);
f[id]=true;
cnt=;
while (i<=m&&h[i]>=len){
id=getid(sa[i]);
if (!f[id]){
f[id]=true;
cnt++;
}
i++;
}
if (out==){
if (*cnt>n) return true;
}
else
if (*cnt>n){
for (k=sa[i-],j=;j<len;k++,j++){
printf("%c",num[k]+'a'-);
}
printf("\n");
} }
return false;
}
int main(){
freopen("tx.in","r",stdin);
int i,j,k;
while (scanf("%d",&n)&&n!=){
scanf("%s",str[]);
if (n==){
printf("%s\n\n",str[]);
continue;
}
sum[]=strlen(str[]);
for (i=;i<n;i++){
scanf("%s",str[i]);
sum[i]=sum[i-]+strlen(str[i])+;
}
for (k=i=;i<n;i++){
for (j=;j<strlen(str[i]);j++){
num[k++]=str[i][j]-'a'+;
}
num[k++]=i;
}
m=k-;
da(num,sa,m+,);
cal(num,m);
int l=,r=m,mid;
while (l<r){
mid=(l+r+)/;
if (check(mid)) l=mid;
else r=mid-;
}
if (l==) printf("?\n\n");
else {
check(l,);
printf("\n");
}
}
}
后缀数组