您的位置:首页 > Web前端

【poj 3294】 Life Forms 后缀数组 *height分组

2016-03-10 21:52 337 查看
、Life Forms

Time Limit: 5000MS Memory Limit: 65536K

Total Submissions: 12433 Accepted: 3482

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

?

题意:给定n个字符串,求出现在不小于k/2个字符串中的最长子串。

SOLUTION:

1.k个字符连接,中“#”+1分隔(保证rank连续不在同一串)

2.二分答案,*height分组判断;时间复杂度为

O(nlogn)

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int n;
int wa[100200],wb[100200],rank[100200],sa[100200],be[100200];
int height[100200];
bool used[105];
int flag=0;
char s[1050];
int nn;
int r[100200];
int cnt[100200];
int pos[100200];
int ans;
void DA(int n,int m)
{
int *x=wa,*y=wb;
for(int i=0;i<m;i++) cnt[i]=0;;
for(int i=0;i<n;i++) cnt[x[i]=r[i]]++;
for(int i=1;i<m;i++) cnt[i]+=cnt[i-1];
for(int i=n-1;i>=0;i--) sa[--cnt[x[i]]]=i;
for(int j=1,p=1;p<n;j<<=1,m=p)
{
p=0;
for(int i=n-j;i<n;i++)   y[p++]=i;
for(int i=0;i<n;i++) if(sa[i]>=j) y[p++]=sa[i]-j;
for(int i=0;i<m;i++)  cnt[i]=0;
for(int i=0;i<n;i++) cnt[x[y[i]]]++;
for(int i=1;i<m;i++) cnt[i]+=cnt[i-1];
for(int i=n-1;i>=0;i--) sa[--cnt[x[y[i]]]]=y[i];
swap(x,y);
x[sa[0]]=0;p=1;
for(int i=1;i<n;i++)
x[sa[i]]=((y[sa[i]]==y[sa[i-1]])&&(y[sa[i]+j]==y[sa[i-1]+j]))?p-1:p++;
}
}
void cal_H(int n)
{
for(int i=1;i<=n;i++) rank[sa[i]]=i;
int j=0,k=0;
for(int i=0;i<n;height[rank[i++]]=k)
for(k?k--:0,j=sa[rank[i]-1];r[i+k]==r[j+k];k++);
return ;
}
bool C(int x)
{
int sum=0,num=0;
memset(used,0,sizeof(used));
for(int i=2;i<=n;i++)
{
if(height[i]>=x)
{

if(used[be[sa[i-1]]]==0) used[be[sa[i-1]]]=1,num++;
if(used[be[sa[i]]]==0) used[be[sa[i]]]=1,num++;
}
else
{
if(num>nn/2) pos[++sum]=sa[i-1];
num=0;
memset(used,0,sizeof(used));
}
}
if(num>nn/2)pos[++sum]=sa
;
if(sum){pos[0]=sum;return true;}
return 0;
}
void solve()
{
ans=0;
int ll=0,rr=n;
while(ll<=rr)
{
int mid=(ll+rr)>>1;
if(C(mid)) ans=mid,ll=mid+1;
else rr=mid-1;
}
}
int main()
{
while(scanf("%d",&nn)&&nn)
{
ans=0;
n=0;
for(int i=1;i<=nn;i++)
{
scanf("%s",s);
for(int j=0;s[j];j++)
{
be
=i;
r[n++]=s[j]-'a'+1;
}
be
=i;
r
=i+200;
if(i!=n) n++;
}
r
=0;
DA(n+1,405);
cal_H(n);
//  for(int i=1;i<=n;i ++)
//  cout<<height[i]<<"   ";
//  cout<<endl;
solve();
//  C(2);
//  cout<<ans<<endl;

if(ans==0)
{
printf("?\n");
puts("");
continue;
}
for(int i=1;i<=pos[0];i++)
{
for(int j=pos[i];j<pos[i]+ans;j++) printf("%c",r[j]+'a'-1);
printf("\n");
}
puts("");
}

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