Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 16223 | Accepted: 4763 |
Descriptionios
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.spa
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.rest
Inputcode
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.orm
Outputblog
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.ip
Sample Inputci
3 abcdefg bcdefgh cdefghi 3 xxx yyy zzz 0
Sample Output字符串
bcdefg cdefgh ?
Source
//論文題,將 n 個字符串連起來,中間用不相同的且沒有出如今字符串中的字符隔開,求後綴數組。而後二分答案,將後綴 //分紅若干組,判斷每組的後綴是否出如今不小於 k 個的原串中。這個作法的時間複雜度爲 O(nlogn)。 //數組要開大一些否則re。 #include<iostream> #include<cstdio> #include<cstring> using namespace std; const int MAXN=300000; int sa[MAXN+9],he[MAXN+9],ra[MAXN+9],xx[MAXN+9],yy[MAXN+9],buc[MAXN+9]; int s[MAXN+9],id[MAXN+9],vis[1009],q[1009]; int len,m,top; void get_suf() { int *x=xx,*y=yy; for(int i=0;i<m;i++) buc[i]=0; for(int i=0;i<len;i++) buc[x[i]=s[i]]++; for(int i=1;i<m;i++) buc[i]+=buc[i-1]; for(int i=len-1;i>=0;i--) sa[--buc[x[i]]]=i; for(int k=1;k<=len;k<<=1){ int p=0; for(int i=len-1;i>=len-k;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++) buc[i]=0; for(int i=0;i<len;i++) buc[x[y[i]]]++; for(int i=1;i<m;i++) buc[i]+=buc[i-1]; for(int i=len-1;i>=0;i--) sa[--buc[x[y[i]]]]=y[i]; swap(x,y); p=1;x[sa[0]]=0; for(int i=1;i<len;i++){ if(y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+k]==y[sa[i]+k]) x[sa[i]]=p-1; else x[sa[i]]=p++; } if(p>=len) break; m=p; } for(int i=0;i<len;i++) ra[sa[i]]=i; int k=0; for(int i=0;i<len;i++){ if(ra[i]==0) { he[0]=0; continue; } if(k) k--; int j=sa[ra[i]-1]; while(s[i+k]==s[j+k]&&i+k<len&&j+k<len) k++; he[ra[i]]=k; } } bool solve(int mid,int n) { memset(vis,0,sizeof(vis)); int l=0,qq[1009],cnt=0,st=-1; for(int i=1;i<len;i++){ if(he[i]<mid){ if(cnt>n/2&&st!=-1) qq[++l]=st; memset(vis,0,sizeof(vis)); cnt=0;st=-1; }else{ if(st==-1) st=i-1; if(!vis[id[sa[i]]]) cnt++; vis[id[sa[i]]]=1; if(!vis[id[sa[i-1]]]) cnt++; vis[id[sa[i-1]]]=1; } } if(cnt>=n/2&&st!=-1) qq[++l]=st; if(l){ top=l; for(int i=1;i<=l;i++) q[i]=qq[i]; return 1; }else return 0; } int main() { int n; char ch[2000]; while(scanf("%d",&n)&&n){ len=0; top=0; int r=0,l=0,ans=0; for(int i=1;i<=n;i++){ scanf("%s",ch); int tmp=strlen(ch); r=max(r,tmp); for(int j=0;j<tmp;j++){ s[len]=ch[j]-'a'; id[len++]=i; } s[len]=i+30; id[len++]=0; } m=200; get_suf(); while(l<=r){ int mid=(l+r)>>1; if(solve(mid,n)) { ans=mid;l=mid+1; } else r=mid-1; } if(ans==0){ printf("?\n\n"); continue; } for(int i=1;i<=top;i++){ for(int j=sa[q[i]];j<=sa[q[i]]+ans-1;j++) printf("%c",s[j]+'a'); printf("\n"); } printf("\n"); } return 0; }