您的位置:首页 > Web前端

POJ 3294 Life Forms (后缀数组求解出现次数不少于K次的串,5级)

2013-09-10 10:11 417 查看
C - Life FormsCrawling in process...Crawling failedTime Limit:5000MSMemory Limit:65536KB 64bit IO Format:%I64d & %I64uSubmitStatusAppoint description:System Crawler (2013-05-30)DescriptionYou 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 shapeslike cubes, oil slicks or clouds of dust.The answer is given in the 146th episode of Star Trek - The Next Generation, titledThe 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.InputStandard 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 thelast test case.OutputFor 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 testcases.Sample Input
3
abcdefg
bcdefgh
cdefghi
3
xxx
yyy
zzz
0
Sample Output
bcdefg
cdefgh

?
思路:把所有的串连起来,求出后缀数组。
然后二分答案长度,判断可行性。
判断可以扫一遍后缀数组的公共前缀,看是否有连续的包含超过一半的字符串。
#include<cstring>#include<cstdio>#include<iostream>#define FOR(i,a,b) for(int i=a;i<=b;++i)#define clr(f,z) memset(f,z,sizeof(f))using namespace std;const int msize=2e5+9;int r[msize];class SUFFIX_ARRAY{public:int sa[msize],rank[msize],t1[msize],h[msize],c[msize];int who[msize];bool vis[109];bool cmp(int*r,int i,int k){return r[ sa[i] ]==r[ sa[i-1] ]&&r[ sa[i]+k ]==r[ sa[i-1]+k ];}void build_SA(int*s,int n,int m){ int*wx=t1,*wy=rank;FOR(i,0,m-1)c[i]=0;FOR(i,0,n-1)++c[ wx[i]=s[i] ];FOR(i,1,m-1)c[i]+=c[i-1];for(int i=n-1;i>=0;--i)sa[ --c[ wx[i] ] ]=i;for(int k=1;k<=n;k<<=1){int p=0;FOR(i,n-k,n-1)wy[p++]=i;FOR(i,0,n-1)if(sa[i]>=k)wy[p++]=sa[i]-k;FOR(i,0,m-1)c[i]=0;FOR(i,0,n-1)++c[ wx[ wy[i] ] ];FOR(i,1,m-1)c[i]+=c[i-1];for(int i=n-1;i>=0;--i)sa[ --c[ wx[ wy[i] ] ] ]=wy[i];swap(wx,wy);p=1;wx[ sa[0] ]=0;FOR(i,1,n-1)wx[ sa[i] ]=cmp(wy,i,k)?p-1:p++;if(p>=n)break;m=p;}}void get_H(int*s,int n){FOR(i,0,n-1)rank[ sa[i] ]=i;int k=0;FOR(i,0,n-1){if(k)--k;int j=sa[rank[i]-1];//if(j<0)continue;while(s[i+k]==s[j+k]){++k;}h[rank[i]]=k;///写成了h[i]=k 严重的错误}}void debug(int n){ puts("sa=");FOR(i,0,n-1)printf("%d ",sa[i]);puts("");puts("rank=");FOR(i,0,n-1)printf("%d ",rank[i]);puts("");puts("h=");FOR(i,0,n-1)printf("%d ",h[i]);puts("");}int check(int all,int len,int K){ int ret=0;clr(vis,0);int tmp=who[sa[1]];if(tmp!=-1&&vis[tmp]==0){++ret;vis[tmp]=1;}if(ret>=K)return 1;FOR(i,2,all){if(h[i]<len){ret=0;clr(vis,0);tmp=who[sa[i]];if(tmp!=-1&&vis[tmp]==0){++ret;vis[tmp]=1;}if(ret>=K)return 1;}else{tmp=who[sa[i]];if(tmp!=-1&&vis[tmp]==0){vis[tmp]=1;++ret;}if(ret>=K)return i;}}return -1;}void out(int all,int len,int K){int ret=0;clr(vis,0);int tmp=who[sa[1]];if(tmp!=-1&&vis[tmp]==0){++ret;vis[tmp]=1;}FOR(i,2,all){if(h[i]<len){if(ret>=K){for(int j=0;j<len;++j)printf("%c",r[ sa[i-1]+j ]);printf("\n");}ret=0;clr(vis,0);tmp=who[sa[i]];if(tmp!=-1&&vis[tmp]==0){++ret;vis[tmp]=1;}}else{tmp=who[sa[i]];if(tmp!=-1&&vis[tmp]==0){vis[tmp]=1;++ret;}}}if(ret>=K){for(int j=0;j<len;++j)printf("%c",r[ sa[all]+j ]);printf("\n");}}void getans(int all,int K,int r[]){int l=1,tr=1003,mid;int ans=-1;while(l<=tr){mid=(l+tr)/2;int x=check(all,mid,K);if(x==-1){tr=mid-1;}else{ ans=max(ans,mid);l=mid+1;}}if(ans<=0)printf("?\n");else out(all,ans,K);}};SUFFIX_ARRAY ty;int allhash;char str[102][1009];int main(){int n,len,cas=0;while(~scanf("%d",&n)){ if(n==0)break;if(cas)printf("\n");++cas;allhash=0;FOR(i,0,n-1){scanf("%s",str[i]);for(int j=0;str[i][j];++j)r[allhash+j]=str[i][j],ty.who[allhash+j]=i;len=strlen(str[i]);r[allhash+len]=i+128;ty.who[allhash+len]=-1;allhash+=len+1;}//FOR(i,0,allhash)printf(" %c",r[i]);puts("");--allhash;r[allhash]=0;ty.build_SA(r,allhash+1,300);ty.get_H(r,allhash);// ty.debug(allhash);ty.getans(allhash,n/2+1,r);}return 0;}
[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐