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.
先把所有字符串接起来求后缀数组,再二分答案。这次要验证的是height值不小于k的每一块中是否出现了超过一半的字符串。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char s[200010];
int sa[200010],rank[200010],height[200010],cnt[200010],t1[200010],t2[200010],
n,l,bel[200010],last[200010],clo;
void init()
{
int i,j,tem;
l=0;
for (i=1;i<=n;i++)
{
scanf("%s",s+l+1);
tem=strlen(s+1);
for (j=l+1;j<=tem;j++)
bel[j]=i;
l=tem+1;
s[l]=i;
bel[l]=0;
s[l+1]=0;
}
}
void make()
{
int i,j,k,m='z',p,*x=t1,*y=t2;
for (i=1;i<=m;i++)
cnt[i]=0;
for (i=1;i<=l;i++)
cnt[x[i]=s[i]]++;
for (i=2;i<=m;i++)
cnt[i]+=cnt[i-1];
for (i=l;i;i--)
sa[cnt[x[i]]--]=i;
for (k=1;k<=l;k<<=1)
{
p=0;
for (i=l-k+1;i<=l;i++)
y[++p]=i;
for (i=1;i<=l;i++)
if (sa[i]-k>=1)
y[++p]=sa[i]-k;
for (i=1;i<=m;i++)
cnt[i]=0;
for (i=1;i<=l;i++)
cnt[x[y[i]]]++;
for (i=2;i<=m;i++)
cnt[i]+=cnt[i-1];
for (i=l;i;i--)
sa[cnt[x[y[i]]]--]=y[i];
swap(x,y);
p=x[sa[1]]=1;
for (i=2;i<=l;i++)
{
if (y[sa[i]]!=y[sa[i-1]]||y[sa[i]+k]!=y[sa[i-1]+k]) p++;
x[sa[i]]=p;
}
if ((m=p)>=l) break;
}
for (i=1;i<=l;i++)
rank[sa[i]]=i;
for (i=1,k=0;i<=l;i++)
{
if (k) k--;
if (rank[i]==1) continue;
while (s[i+k]==s[sa[rank[i]-1]+k]) k++;
height[rank[i]]=k;
}
}
bool ok(int k)
{
int i,now=0;
for (i=1;i<=l;i++)
if (height[i]<k)
{
now=1;
last[bel[sa[i]]]=++clo;
}
else
{
if (last[bel[sa[i]]]<clo)
{
last[bel[sa[i]]]=clo;
now++;
if (now>n/2) return 1;
}
}
return 0;
}
void out(int k)
{
int i,j,now=0,flag=0;
for (i=1;i<=l;i++)
if (height[i]<k)
{
now=1;
flag=0;
last[bel[sa[i]]]=++clo;
}
else
{
if (last[bel[sa[i]]]<clo)
{
last[bel[sa[i]]]=clo;
now++;
if (now>n/2&&!flag)
{
flag=1;
for (j=1;j<=k;j++)
putchar(s[sa[i]+j-1]);
putchar('\n');
}
}
}
}
int main()
{
int ll,rr,mid;
bool flag=0;
while (scanf("%d",&n)&&n)
{
if (flag) putchar('\n');
flag=1;
if (n==1)
{
scanf("%s",s+1);
printf("%s\n",s+1);
continue;
}
init();
make();
ll=0;
rr=l;
while (ll<rr)
{
mid=(ll+rr+1)/2;
if (ok(mid)) ll=mid;
else rr=mid-1;
}
if (!ll) printf("?\n");
else out(ll);
}
}