字符串(后缀数组):POJ 3294 Life Forms

Life Forms

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 <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn=;
char s[maxn];
int r[maxn],Wa[maxn],Wb[maxn],Wv[maxn],Ws[maxn];
int rank[maxn],lcp[maxn],belong[maxn],sa[maxn];
bool cmp(int *p,int i,int j,int l){
return p[i]==p[j]&&p[i+l]==p[j+l];
}
void DA(int n,int m){
int i,j,p,*x=Wa,*y=Wb,*t;
for(i=;i<m;i++)Ws[i]=;
for(i=;i<n;i++)++Ws[x[i]=r[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;m=p,j<<=){
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++)++Ws[Wv[i]=x[y[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,x[sa[]]=,i=,p=;i<n;i++)
x[sa[i]]=cmp(y,sa[i],sa[i-],j)?p-:p++;
}
} void LCP(int n){
int i,j,k=;
for(i=;i<=n;i++)rank[sa[i]]=i;
for(i=;i<n;lcp[rank[i++]]=k)
for(k?k--:k,j=sa[rank[i]-];r[i+k]==r[j+k];k++);
}
int tot,tim,vis[maxn];
bool Judge(int n,int x,int g){
int tmp=;++tim;
for(int i=;i<=n;i++){
if(lcp[i]<x)
tmp=,++tim;
else{
if(vis[belong[sa[i]]]!=tim)
tmp++,vis[belong[sa[i]]]=tim;
if(vis[belong[sa[i-]]]!=tim)
tmp++,vis[belong[sa[i-]]]=tim;
if(tmp>g)return true;
}
}
return false;
} void Solve(int n,int x,int g){
int tmp=,tag=;++tim;
for(int i=;i<=n;i++){
if(lcp[i]<x)
tmp=,++tim,tag=;
else{
if(vis[belong[sa[i]]]!=tim)
tmp++,vis[belong[sa[i]]]=tim;
if(vis[belong[sa[i-]]]!=tim)
tmp++,vis[belong[sa[i-]]]=tim;
if(tmp>g&&!tag){
for(int j=;j<x;j++)
printf("%c",r[sa[i-]+j]);
printf("\n");
tag=;
}
}
}
return;
} int main(){
while(~scanf("%d",&tot)&&tot){ int len=,lo=,hi=;
for(int i=;i<=tot;i++){
scanf("%s",s);
for(int j=;s[j];j++){
belong[len]=i;
r[len++]=s[j];
if(!s[j+])hi=min(hi,j+);
}
belong[len]=+i;
r[len++]='z'+i;
}
r[len]=;
DA(len+,);
LCP(len); while(lo<=hi){
int mid=(lo+hi)>>;
if(Judge(len,mid,tot/))lo=mid+;
else hi=mid-;
}
if(hi>=)
Solve(len,hi,tot/);
else
printf("?\n");
printf("\n");
}
return ;
}
上一篇:android获取系统版本号


下一篇:daterangepicker日历插件使用参数注意问题