poj3294 --Life Forms

Life Forms
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 12483   Accepted: 3501

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

?




瘠薄
  1 #include<cstdio>
  2 #include<cstring>
  3 #include<cmath>
  4 #include<string>
  5 #include<algorithm>
  6 #include<iostream>
  7 #define maxn 200005
  8 int ws[maxn],wa[maxn],sa[maxn],num[maxn],n,wv[maxn],rank[maxn];
  9 int h[maxn],wb[maxn],sum[maxn],m;
 10 char str[105][1005];
 11 bool cmp(int *r,int a,int b,int l){
 12     return r[a]==r[b]&&r[a+l]==r[b+l];
 13 }
 14 
 39 void da(int *r,int *sa,int n,int m){
 40     int *t,*x=wa,*y=wb,i,j,p;
 41     for (i=0;i<m;i++) ws[i]=0;
 42     for (i=0;i<n;i++) x[i]=r[i];
 43     for (i=0;i<n;i++) ws[x[i]]++;
 44     for (i=1;i<m;i++) ws[i]+=ws[i-1];
 45     for (i=n-1;i>=0;i--) sa[--ws[x[i]]]=i;
 46     for (j=1,p=1;p<n;j*=2,m=p){
 47         for (p=0,i=n-j;i<n;i++) y[p++]=i;
 48         for (i=0;i<n;i++) if (sa[i]-j>=0) y[p++]=sa[i]-j;
 49         for (i=0;i<m;i++) ws[i]=0;
 50         for (i=0;i<n;i++) wv[i]=x[y[i]];
 51         for (i=0;i<n;i++) ws[wv[i]]++;
 52         for (i=1;i<m;i++) ws[i]+=ws[i-1];
 53         for (i=n-1;i>=0;i--) sa[--ws[wv[i]]]=y[i];
 54         for (t=x,x=y,y=t,i=1,p=1,x[sa[0]]=0;i<n;i++)
 55          x[sa[i]]=cmp(y,sa[i],sa[i-1],j)?p-1:p++;
 56     } 
 57 }
 58 void cal(int *r,int n){
 59     int i,j,k=0;
 60     for (int i=1;i<=n;i++) rank[sa[i]]=i;
 61     for (int i=0;i<n;h[rank[i++]]=k)
 62      for (k?k--:0,j=sa[rank[i]-1];r[i+k]==r[j+k];k++);
 63 }
 64 int getid(int k){
 65     int l=0,r=n-1,mid;
 66     while (l<r){
 67         mid=(l+r)/2;
 68         if (sum[mid]<k) {
 69             l=mid+1;
 70         }
 71         else
 72             r=mid;
 73     }
 74     return l;
 75 }
 76 
 77 bool check(int len,int out=0){
 78     int i=n+1,j,k,id,cnt;
 79     bool f[maxn];
 80     while (1){
 81         while (i<=m&&h[i]<len) i++;
 82         if (i>m) break;
 83         memset(f,0,sizeof f);
 84         id=getid(sa[i-1]);
 85         f[id]=true;
 86         cnt=1;
 87         while (i<=m&&h[i]>=len){
 88             id=getid(sa[i]);
 89             if (!f[id]){
 90                 f[id]=true;
 91                 cnt++;
 92             }
 93             i++;
 94         }
 95         if (out==0){
 96             if (2*cnt>n) return true;
 97          } 
 98             else
 99             if (2*cnt>n){
100                 for (k=sa[i-1],j=0;j<len;k++,j++){
101                     printf("%c",num[k]+'a'-100);
102                 }
103                 printf("\n");
104             }
105         
106     }
107     return false;
108 }
144 int main(){
145     freopen("tx.in","r",stdin);
146     int i,j,k;
147     while (scanf("%d",&n)&&n!=0){
148         scanf("%s",str[0]);
149         if (n==1){
150             printf("%s\n\n",str[0]);
151             continue;
152         }
153         sum[0]=strlen(str[0]);
154         for (i=1;i<n;i++){
155             scanf("%s",str[i]);
156             sum[i]=sum[i-1]+strlen(str[i])+1;
157         }
158         for (k=i=0;i<n;i++){
159             for (j=0;j<strlen(str[i]);j++){
160                 num[k++]=str[i][j]-'a'+100;
161             }
162             num[k++]=i;
163         }
164         m=k-1;
165         da(num,sa,m+1,130);
166         cal(num,m);
167         int l=0,r=m,mid;
168         while (l<r){
169             mid=(l+r+1)/2;
170             if (check(mid)) l=mid;
171             else r=mid-1;
172         }
173         if (l==0) printf("?\n\n");
174         else {
175             check(l,1);
176             printf("\n");
177         }
178     }
179 }

 

后缀数组
posted @ 2016-03-30 16:50  GFY  阅读(242)  评论(0编辑  收藏  举报