poj 3294 Life Forms
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 18777 | Accepted: 5530 |
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 ?
Source
1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #include<cmath> 5 #include<algorithm> 6 using namespace std; 7 int const N=100000+1000; 8 int sa[N],wa[N<<1],wb[N<<1],rk[N],num[N],h[N],wv[N],vis[101],id[N],s[N]; 9 char s1[1003]; 10 int cmp(int *r,int x,int y,int z){ 11 return r[x]==r[y] && r[x+z]==r[y+z]; 12 } 13 void build_sa(int *r,int *sa,int n,int m){ 14 int i,j,p,*x=wa,*y=wb; 15 for(i=0;i<m;i++) num[i]=0; 16 for(i=0;i<n;i++) num[x[i]=r[i]]++; 17 for(i=1;i<m;i++) num[i]+=num[i-1]; 18 for(i=n-1;i>=0;i--) sa[--num[x[i]]]=i; 19 for(j=1,p=1;p<n;j<<=1,m=p){ 20 for(p=0,i=n-j;i<n;i++) y[p++]=i; 21 for(i=0;i<n;i++) if(sa[i]>=j) y[p++]=sa[i]-j; 22 for(i=0;i<m;i++) num[i]=0; 23 for(i=0;i<n;i++) wv[i]=x[y[i]]; 24 for(i=0;i<n;i++) num[wv[i]]++; 25 for(i=1;i<m;i++) num[i]+=num[i-1]; 26 for(i=n-1;i>=0;i--) sa[--num[wv[i]]]=y[i]; 27 swap(x,y); 28 for(i=1,p=1,x[sa[0]]=0;i<n;i++) 29 x[sa[i]]=cmp(y,sa[i],sa[i-1],j)? p-1: p++; 30 } 31 for(i=0;i<n;i++) rk[i]=x[i]; 32 } 33 void build_h(int *r,int *sa,int n){ 34 int k=0; 35 for(int i=0;i<n;i++){ 36 if(k) k--; 37 int j=sa[rk[i]-1]; 38 while (r[i+k]==r[j+k]) k++; 39 h[rk[i]]=k; 40 } 41 } 42 int check(int mid,int n,int tot){ 43 if(tot==1) return 1; 44 int num=0; 45 memset(vis,0,sizeof(vis)); 46 for(int i=1;i<=n;i++){ 47 if(h[i]>=mid){ 48 if(!vis[id[sa[i-1]]]){ 49 num++; vis[id[sa[i-1]]]=1; 50 } 51 if(!vis[id[sa[i]]]){ 52 num++; vis[id[sa[i]]]=1; 53 } 54 if(num>tot/2) return 1; 55 }else { 56 num=0; 57 memset(vis,0,sizeof(vis)); 58 } 59 } 60 return 0; 61 } 62 int solve(int mid,int n,int tot){ 63 int num=0; 64 memset(vis,0,sizeof(vis)); 65 for(int i=1;i<=n;i++){ 66 if(h[i]>=mid){ 67 if(!vis[id[sa[i-1]]]){ 68 num++; vis[id[sa[i-1]]]=1; 69 } 70 if(!vis[id[sa[i]]]){ 71 num++; vis[id[sa[i]]]=1; 72 } 73 }else { 74 if(num>tot/2) { 75 for(int j=sa[i-1];j<sa[i-1]+mid;j++) printf("%c",s[j]); 76 printf("\n"); 77 } 78 num=0; 79 memset(vis,0,sizeof(vis)); 80 } 81 } 82 if(num>tot/2) { 83 for(int j=sa[n];j<sa[n]+mid;j++) printf("%c",s[j]); 84 printf("\n"); 85 } 86 } 87 int main(){ 88 int n; int t=0; 89 while (scanf("%d",&n)!=EOF && n){ 90 if(t++) printf("\n"); 91 memset(s,0,sizeof(s)); 92 int sum=130,len=0; 93 for(int i=0;i<n;i++){ 94 scanf("%s",s1); 95 int l=len; 96 int k=strlen(s1); 97 len+=k+1; 98 for(int j=l;j<len-1;j++) id[j]=i+1,s[j]=s1[j-l]; 99 s[len-1]=sum++; 100 } 101 build_sa(s,sa,len+1,sum); 102 build_h(s,sa,len); 103 int l=0,r=1000,ans=0; 104 while (l<=r){ 105 int mid=(l+r)/2; 106 if(check(mid,len,n)) { 107 ans=mid; 108 l=mid+1; 109 }else r=mid-1; 110 } 111 if(ans==0) printf("?\n"); 112 else solve(ans,len,n); 113 } 114 return 0; 115 }