poj3294Life Forms

    技术2022-05-19  22

    Life Forms Time Limit: 5000MS Memory Limit: 65536KTotal Submissions: 4628 Accepted: 1207

    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

    Waterloo Local Contest, 2006.9.30

     

    /* 倍增算法 */ #include<cstdio> #include<cstring> const int maxl=1010; const int maxn=101; bool vis[110]; int num[110]; #define max maxl*maxn int w[max],wa[max],wb[max],wv[max]; int sa[max],height[max],rank[max]; int r[max],n,t,len,a[maxn]; char s[maxl]; 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 *sa,int n,int m) { int i,j,p,*x=wa,*y=wb,*t; for (i=0; i<m; i++) w[i]=0; for (i=0; i<n; i++) w[x[i]=r[i]]++; for (i=1; i<m; i++) w[i]+=w[i-1]; for (i=n-1; i>=0; i--) sa[--w[x[i]]]=i; for (p=1,j=1; p<n; m=p,j*=2) { 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<m; i++) w[i]=0; for (i=0; i<n; i++) w[wv[i]=x[y[i]]]++; for (i=1; i<m; i++) w[i]+=w[i-1]; for (i=n-1; i>=0; i--) sa[--w[wv[i]]]=y[i]; for (t=x,x=y,y=t,x[sa[0]]=0,p=1,i=1; i<n; i++) x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++; } return; } void cal(int *r,int *sa,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++); return; } int caa(int x)//找到子串的位置 { for(int i=1,len=num[1]; i<=t; ++i) { if(x==len) return 0; else if(x<len) return i; len=len+num[i+1]+1; } } bool check(int k)//注意绝对不要把自己加入进去区分两个串的字符算进去 { int x,d=0,i; memset(vis,0,sizeof(vis)); vis[0]=1;//放置自己加进去的特殊字符的 //printf("mid=%d n=%d t=%d/n",k,n,t); x=caa(sa[0]); if(vis[x]==0) d++; for(i=1; i<=n; ++i) { if(height[i]<k) { d=0; memset(vis,0,sizeof(vis)); vis[0]=1; //printf("%d/n",i); } x=caa(sa[i]); if(vis[x]==0) d++; if(d>t/2) return 1; //printf("x=%d/n",x); vis[x]=1; } return 0; } void print(int k) { int x,d=0,i; memset(vis,0,sizeof(vis)); vis[0]=1;//放置自己加进去的特殊字符的 //printf("mid=%d n=%d t=%d/n",k,n,t); x=caa(sa[0]);//对第一个进行初始化 if(vis[x]==0) d++; for(i=1; i<=n; ++i) { if(height[i]<k) { if(d>t/2) { for(int j=sa[i-1]; j<sa[i-1]+k; ++j) printf("%c",r[j]); puts(""); } d=0; memset(vis,0,sizeof(vis)); vis[0]=1; //printf("%d/n",i); } x=caa(sa[i]); if(vis[x]==0) d++; vis[x]=1; } } int rmax; void solve() { int left=1,right=rmax; int ans=0; while(left<=right) { int mid=(left+right)>>1; if(check(mid)) { ans=mid; left=mid+1; } else { right=mid-1; } } if(ans==0) printf("?/n"); else print(ans); } int main() { int cas=0; while(scanf("%d",&t)!=EOF) { if(t==0) break; memset(num,0,sizeof(num)); if(cas==0) cas=1;//输出空行 else printf("/n"); int len=0; n=0; rmax=-1; for(int i=1; i<=t; ++i) //i<100,lower case letter { scanf("%s",s); len=strlen(s); num[i]=len; if(len>rmax) rmax=len; for(int j=0; j<len; ++j) r[n+j]=(int)s[j]; r[n+len]=i+ 200; n=n+len+1;//用不可能出现的符号分割各个串 } if(t==1) { printf("%s/n",s); continue; } da(r,sa,n+1,350); //printf("2222222/n"); cal(r,sa,n); //printf("1111111111111/n"); solve(); // puts(""); } return 0; } 


    最新回复(0)