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<cstring>
#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
const int N = 400000+5;
bool vis[110];
int wa[N],wb[N],wv[N],c[N],height[N],sa[N],pos[N],len[110],ranks[N],s[N];
char str[N];
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 i,j,p,*x=wa,*y=wb,*t;
for(i=0;i<m;i++) c[i]=0;
for(int i=0;i<n;i++) c[x[i]=r[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(j=1,p=1;p<n;j*=2,m=p){
for(p=0,i=n-j;i<n;i++) y[p++]=i;
for(i=0;i<n;i++) if(sa[i]>=j) y[p++]=sa[i]-j;
for(i=0;i<n;i++) wv[i]=x[y[i]];
for(i=0;i<m;i++) c[i]=0;
for(i=0;i<n;i++) c[wv[i]]++;
for(i=1;i<m;i++) c[i]+=c[i-1];
for(i=n-1;i>=0;i--) sa[--c[wv[i]]]=y[i];
for(t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1;i<n;i++)
x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
}
return ;
}
void calheight(int *r,int *sa,int n){
int i,j,k=0;
for(i=1;i<=n;i++) ranks[sa[i]]=i;
for(i=0;i<n;height[ranks[i++]]=k)
for(k?k--:0,j=sa[ranks[i]-1];r[i+k]==r[j+k];k++);
return ;
}
bool judge(int mid,int n,int k){
memset(vis,0,sizeof(vis));
int num=0,ans=0;
for(int i=1;i<=n;++i){
if(height[i]>=mid){
for(int j=1;j<=k;++j){
if(sa[i]>len[j-1]&& sa[i]<len[j]){
ans+=(!vis[j]);
vis[j]=1;
}
if(sa[i-1]>len[j-1]&&sa[i-1]<len[j]){
ans+=(!vis[j]);
vis[j]=1;
}
}
}
else{
if(ans>k/2)pos[++num]=sa[i-1];
ans=0;
memset(vis,0,sizeof(vis));
}
}
if(ans>k/2)pos[++num]=sa[n];
if(num){
pos[0]=num;
return true;
}
return false;
}
int main(){
int k,n,flag=0,i,j;len[0]=-1;
while(scanf("%d",&k),k){
pos[0]=n=0;int t=30;
for(i=1;i<=k;++i){
scanf("%s",str);
int l=strlen(str);
for(j=0;j<l;++j){
s[n++]=str[j]-'a'+1;
}
len[i]=n;
s[n++]=t++;
}
s[n-1]=0;
da(s,sa,n,t);
calheight(s,sa,n-1);
int l=1,r=n,mid;
while(l<=r){
mid=(l+r)>>1;
if(judge(mid,n,k))l=mid+1;
else r=mid-1;
}
if(flag++)printf("\n");
if(l-1 == 0)printf("?\n");
else{
for(i=1;i<=pos[0];++i){
for(j=pos[i];j<pos[i]+l-1;++j)
printf("%c",s[j]+'a'-1);
printf("\n");
}
}
}
return 0;
}