传送门
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
题目大意
有多组数据,每组数据包括n个字符串,求其中在超过一半的字符串中出现过且最长的子串,如有多解按照字典序依次输出,若无解输出’?’(不包括引号)。
题解
人生中的第一道后缀数组题,调了一天【神犇请轻喷
将所有字符串接到一起,中间加一个从未出现过的字符(且各不相同),在sa数组上二分答案长度,然后在height数组上进行判断。
lrj的代码中有许多地方去掉一样是可以A的,而且速度更快,下方代码已经删除多余部分,如有神犇能指出错误蒟蒻将感激不尽。
CODE:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1e5+110;
struct SA
{
int s[N];
int t1[N],t2[N],sa[N],height[N],c[N],rank[N];
int len;
inline void clear(){len=0;memset(sa,0,sizeof(sa));}
inline void makesa(int m)
{
int *x=t1,*y=t2;
for(int i=0;i<m;i++) c[i]=0;
for(int i=0;i<len;i++) c[x[i]=s[i]]++;
for(int i=1;i<m;i++) c[i]+=c[i-1];
for(int i=len-1;i>=0;i--) sa[--c[x[i]]]=i;
for(int k=1;k<=len;k<<=1)
{
int p=0;
for(int i=len-k;i<len;i++) y[p++]=i;
for(int i=0;i<len;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<len;i++) c[x[y[i]]]++;
for(int i=1;i<m;i++) c[i]+=c[i-1];
for(int i=len-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<len;i++)
x[sa[i]]=y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+k]==y[sa[i]+k]?p-1:p++;
if(p>=len) break;
m=p;
}
}
inline void makeheight()
{
for(int i=0;i<len;i++) rank[sa[i]]=i;
int k=0;
for(int i=0;i<len;i++)
if(rank[i])
{
if(k) k--;
int j=sa[rank[i]-1];
while(s[i+k]==s[j+k]) k++;
height[rank[i]]=k;
}
}
}a;
int id[N];
int flag[N];
char t[N];
int n,len,num,maxlen;
inline int max(int a,int b){return a>b?a:b;}
inline bool check(int L,int R)
{
if(R-L<=n/2) return 0;
memset(flag,0,sizeof(flag));
int tot=0;
for(int i=L;i<R;i++)
{
int x=id[a.sa[i]];
if(!flag[x]) tot++,flag[x]=1;
}
return tot>n/2;
}
inline void print(int L,int R)
{
for(int i=L;i<R;i++)
printf("%c",a.s[i]+'a'-1);
printf("\n");
}
inline bool printans(int len,bool can)
{
int L=0;
for(int R=1;R<a.len;R++)
if(a.height[R]<len)
{
if(check(L,R))
if(can) print(a.sa[L],a.sa[L]+len);
else return 1;
L=R;
}
return 0;
}
inline void solve(int maxlen)
{
if(!printans(1,0)){puts("?");return;}
int L=1,R=maxlen,mid;
while(L<R)
{
mid=(L+R+1)>>1;
if(printans(mid,0)) L=mid;
else R=mid-1;
}
printans(L,1);
}
inline void add(int ch,int belong)
{
id[a.len]=belong;
a.s[a.len++]=ch;
}
int main()
{
while(scanf("%d",&n)==1&&n)
{
if(num++) printf("\n");
a.clear();maxlen=0;
for(int i=0;i<n;i++)
{
scanf("%s",t);
len=strlen(t);
maxlen=max(maxlen,len);
for(int j=0;j<len;j++)
add(t[j]-'a'+1,i);
add(100+i,n);
}
add(0,n);
a.makesa(100+n);
a.makeheight();
solve(maxlen);
}
return 0;
}