您的位置:首页 > Web前端

POJ 3294 Life Forms

2014-03-01 18:48 218 查看
D - Life Forms
Time Limit:5000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Submit Status Practice POJ
3294

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个串中找出最长的在多于一半的串里面出现过的子串,如果有多个的话按字典序输出。

我们把问题分解开来。第一步来寻找出现过的次数满足条件的最长子串。

首先,我们通过特殊字符'$'来连接字符串。二分最后出现的字符串的长度。如果该长度满足要求,那么在height数组中就会有值大于等于该长度的一段,再将这段长度与出现次数相比较,就可以进行判定。

再来解决字典序的问题,我们始终要注意到,sa数组是保证字典序的相当强的工具。所以最后直接按sa数组的顺序输出即可。

#include<cstdio>
#include<cstring>
using namespace std;
const int nMax = 111111;

int  num[nMax];
char s[nMax];
int sa[nMax], rank[nMax], height[nMax];
int wa[nMax], wb[nMax], wv[nMax], wd[nMax];

int loc[150];

int 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 n, int m){
int i, j, p, *x = wa, *y = wb, *t;
for(i = 0; i < m; i ++) wd[i] = 0;
for(i = 0; i < n; i ++) wd[x[i]=r[i]] ++;
for(i = 1; i < m; i ++) wd[i] += wd[i-1];
for(i = n-1; i >= 0; i --) sa[-- wd[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 ++) wd[i] = 0;
for(i = 0; i < n; i ++) wd[wv[i]] ++;
for(i = 1; i < m; i ++) wd[i] += wd[i-1];
for(i = n-1; i >= 0; i --) sa[-- wd[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 ++;
}
}
}

void calHeight(int *r, int n){
int i, j, k = 0;
for(i = 1; i <= n; i ++) rank[sa[i]] = i;
for(i = 0; i < n; height[rank[i ++]] = k){
for(k ? k -- : 0, j = sa[rank[i]-1]; r[i+k] == r[j+k]; k ++);
}
}

int find(int l){
for(int i=0;i<111;i++)  if(l<loc[i])  return i;
return -1;
}

bool vis[150];
int check(int mid,int n,int len){
int flag=n/2+1;
int cnt;
cnt=0;
memset(vis,false,sizeof(vis));
for(int i=2;i<=len;i++){
if(height[i]<mid){
cnt=0;
memset(vis,0,sizeof(vis));
}else{
if(!vis[find(sa[i-1])]){
cnt++;
vis[find(sa[i-1])]=true;
}
if(!vis[find(sa[i])]){
cnt++;
vis[find(sa[i])]=true;
}
if(cnt>=flag)  return 1;
}
}
return 0;
}

int main(){
int n;
int flag=0;
while(scanf("%d",&n)!=EOF){
if(n==0)  break;
if(flag)  printf("\n");
else  flag=1;
int cnt=0,len;
for(int i=0;i<n;i++){
scanf("%s",s+cnt);
len=(int)strlen(s);
s[len]='0'+cnt%10;
loc[i]=len++;
cnt=len;
}
if(n==1){
for(int i=0;i<len-1;i++)  printf("%c",s[i]);
printf("\n");
continue;
}
//puts(s);
for(int i=0;i<len;i++)  num[i]=s[i]+100;
//for(int i=0;i<len;i++)  printf("%d ",num[i]);
//printf("\n");
num[len]=0;
da(num,len+1,250);
calHeight(num,len);
int left=0,right=len,mid;
int ans;
while(right>=left){
mid=(left+right)/2;
if(check(mid,n,len)){
ans=mid;
left=mid+1;
}else{
right=mid-1;
}
}
//printf("%d\n",ans);
if(ans==0)  printf("?\n");
else{
int tt=n/2+1;
int cnt;
int tflag=0;
cnt=0;
memset(vis,false,sizeof(vis));
for(int i=2;i<=len;i++){
if(height[i]<ans){
cnt=0;
memset(vis,0,sizeof(vis));
tflag=0;
}else{
if(!vis[find(sa[i-1])]){
cnt++;
vis[find(sa[i-1])]=true;
}
if(!vis[find(sa[i])]){
cnt++;
vis[find(sa[i])]=true;
}
if(cnt>=tt&&tflag==0){
for(int j=sa[i];j<sa[i]+ans;j++)  printf("%c",s[j]);
printf("\n");
tflag=1;
}
}
}
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: