标签:
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 11178 | Accepted: 3085 |
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
就是找超过k/2个的最长公共子串
poj第200道题,,这个艰辛啊。。。本来很简单的一道题,超时一下午,,无语死了,vis数组开大了,换成bool型又快了800ms
ac代码
Problem: 3294 User: kxh1995 Memory: 6196K Time: 1266MS Language: C++ Result: Accepted
#include<stdio.h> #include<string.h> #include<algorithm> #include<iostream> #define min(a,b) (a>b?b:a) #define max(a,b) (a>b?a:b) #define N 1000005 using namespace std; char str[1000010]; int sa[1000010],Rank[1000010],rank2[1000010],height[1000010],c[1000010],*x,*y,s[1000010],k; void cmp(int n,int sz) { int i; memset(c,0,sizeof(c)); for(i=0;i<n;i++) c[x[y[i]]]++; for(i=1;i<sz;i++) c[i]+=c[i-1]; for(i=n-1;i>=0;i--) sa[--c[x[y[i]]]]=y[i]; } void build_sa(int *s,int n,int sz) { x=Rank,y=rank2; int i,j; for(i=0;i<n;i++) x[i]=s[i],y[i]=i; cmp(n,sz); int len; for(len=1;len<n;len<<=1) { int yid=0; for(i=n-len;i<n;i++) { y[yid++]=i; } for(i=0;i<n;i++) if(sa[i]>=len) y[yid++]=sa[i]-len; cmp(n,sz); swap(x,y); x[sa[0]]=yid=0; for(i=1;i<n;i++) { if(y[sa[i-1]]==y[sa[i]]&&sa[i-1]+len<n&&sa[i]+len<n&&y[sa[i-1]+len]==y[sa[i]+len]) x[sa[i]]=yid; else x[sa[i]]=++yid; } sz=yid+1; if(sz>=n) break; } for(i=0;i<n;i++) Rank[i]=x[i]; } void getHeight(int *s,int n) { int k=0; for(int i=0;i<n;i++) { if(Rank[i]==0) continue; k=max(0,k-1); int j=sa[Rank[i]-1]; while(s[i+k]==s[j+k]) k++; height[Rank[i]]=k; } } int len[110],anssize,ans[1000010]; bool vis[105]; int judge(int n,int mid) { int i,j; int cnt=0; int size=0; memset(vis,0,sizeof(vis)); for(i=1;i<n;i++) { if(height[i]>=mid) { for(j=1;j<=k;j++) { if(sa[i]>len[j-1]&&sa[i]<len[j]) { if(!vis[j]) { cnt++; vis[j]=1; } } if(sa[i-1]>len[j-1]&&sa[i-1]<len[j]) { if(!vis[j]) { cnt++; vis[j]=1; } } } } else { if(cnt>k/2) ans[++size]=sa[i-1]; cnt=0; memset(vis,0,sizeof(vis)); } } if(cnt>k/2) ans[++size]=sa[n]; if(size) { anssize=size; return 1; } return 0; } int main() { //int k; int flag=0; while(scanf("%d",&k)!=EOF,k) { int i,ll=0,j; int num=0; for(i=1;i<=k;i++) { scanf("%s",str+ll); for(;str[ll];ll++) s[ll]=str[ll]; s[ll]='#'+i; len[++num]=ll; ll++; } s[ll-1]=0; build_sa(s,ll,255); getHeight(s,ll-1); int l=0,r=ll; while(l<=r) { int mid=(l+r)>>1; if(judge(ll,mid)) { l=mid+1; } else r=mid-1; } if(flag) printf("\n"); flag=1; if(l<2) { printf("?\n"); } else { for(i=1;i<=anssize;i++) { for(j=0;j<l-1;j++) { printf("%c",str[ans[i]+j]); } printf("\n"); } } } }
版权声明:本文为博主原创文章,未经博主允许不得转载。
POJ 题目3294Life Forms(后缀数组求超过k个的串的最长公共子串)
标签:
原文地址:http://blog.csdn.net/yu_ch_sh/article/details/48006243