您的位置:首页 > Web前端

Life Forms POJ - 3294 后缀数组

2017-09-20 18:05 435 查看
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

?


题意:给你n个串,让你找出出现次数>n/2的最长子串


思路:容易想得到 需要二分答案k,对于每一个k都把字符串分组,看一下这一组是否满足条件。

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<complex>
#include<queue>
using namespace std;
const int MAXN=1e6+10;
char temp[MAXN];
int s[MAXN];
char ans[1999][120];
int t1[MAXN],t2[MAXN],cc[MAXN],x[MAXN],sa[MAXN],Rank[MAXN],height[MAXN],range[MAXN],n;
int len,cnt_ans,ans1;
bool cnt[1000];
bool cmp(int *y,int a,int b,int k)
{
int a1=y[a];
int b1=y[b];
int a2=a+k>=len ? -1:y[a+k];
int b2=b+k>=len ? -1:y[b+k];
return a1==b1 && a2==b2;
}
void make_sa()
{
int *x=t1,*y=t2;
int m=1000;
for(int i=0; i<m; i++) cc[i]=0;
for(int i=0; i<len; i++) ++cc[x[i]=s[i]];
for(int i=1; i<m; i++) cc[i]+=cc[i-1];
for(int i=len-1; i>=0; i--) sa[--cc[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++) cc[i]=0;
for(int i=0; i<len; i++) ++cc[x[y[i]]];
for(int i=1; i<m; i++) cc[i]+=cc[i-1];
for(int i=len-1; i>=0; i--) sa[--cc[x[y[i]]]]=y[i];
swap(x,y);
m=1;
x[sa[0]]=0;
for(int i=1; i<len; i++)
x[sa[i]]=cmp(y,sa[i],sa[i-1],k) ? m-1:m++;
if( m>=len ) break;
}
}
void make_height()
{
for(int i=0; i<len; i++) Rank[sa[i]]=i;
height[0]=0;
int k=0;
for(int i=0; i<len; i++)
{
if(!Rank[i]) continue;
int j=sa[Rank[i]-1];
if(k) k--;
while(s[i+k]==s[j+k])k++;
height[Rank[i]]=k;
}
}
bool judge(int k)
{
int pos1,pos2,sum,flag,flagx;
flag=1,flagx=0;
for(int i=0; i<len; i++)
{
if(height[i]<k)
{
memset(cnt,0,sizeof(cnt)),sum=0,flag=1;
continue;
}
if(s[sa[i]]>=26)
continue;
pos1=lower_bound(range+1,range+1+n,sa[i])-(range+1);
pos2=lower_bound(range+1,range+1+n,sa[i-1])-(range+1);///确定位置
if(cnt[pos1]!=1)
{
sum++;
cnt[pos1]=1;
}
if(cnt[pos2]!=1)
{
sum++;
cnt[pos2]=1;
}
if(sum>n/2&&flag)
{
flagx=1;
if(k>ans1)
cnt_ans=0,ans1=k;
for(int j=0; j<k; j++)
{
ans[cnt_ans][j]=s[j+sa[i]]+'a';
}
ans[cnt_ans++][k]='\0';
flag=0;
}
}
if(flagx)
return 1;
return 0;
}
int main()
{
while(scanf("%d",&n)!=EOF&&n)
{
int start=50;///未出现的字符
len=0;
for(int i=1; i<=n; i++)
{
scanf("%s",temp);
int lentemp=strlen(temp);
for(int j=0; j<lentemp; j++)
{
s[len++]=temp[j]-'a';///数字化
}
range[i]=len;
s[len++]=start+i;
}
make_sa();
make_height();
int l=1,r=10086;
ans1=-1,cnt_ans=0;
while(l<=r)
{
memset(cnt,0,sizeof(cnt));
int mid=(l+r)/2;
int ks;
if(ks=judge(mid))
{
l=mid+1;
}
else
r=mid-1;
}
if(ans1==-1)
printf("?\n");
else
for(int i=0; i<cnt_ans; i++)
puts(ans[i]);
putchar('\n');

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: