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 0Sample 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]
相关文章推荐
- POJ 3294 Life Forms (后缀数组求解出现次数不少于K次的串,5级)
- poj 3261 二分答案+后缀数组 求至少出现k次的最长重复子序列
- [poj 3693]后缀数组+出现次数最多的重复子串
- poj 2406 求连续重复子串出现的次数 后缀数组
- poj 3882 后缀数组 求一个串至少出现k次的最长重复子串的长度
- poj 2945 trie树统计字符串出现次数
- POJ 3294 Life Forms (后缀数组,求出现在不少于k个字符串的最长子串)
- (Realx 数论1.28)POJ 2282 The Counting Problem(区间统计数字:统计a、b之间各个数字(0~9)出现的次数)
- POJ 3461 Oulipo【KMP】【模板题】(找出第一个字符串在第二个字符串中出现次数)
- Google面试算法题:《从1到n的正数中1出现的次数》的求解
- Hdu 4552 怪盗基德的挑战书 (所有前缀出现次数和 后缀数组或KMP)
- POJ 2406 Power Strings (求字符串循环节出现的次数)
- POJ 3461 Oulipo (KMP,求模版串在文本串中可覆盖出现的次数,constructive)
- (Relax 数论1.30)POJ 3252 Round Numbers(计算a-b中round number的个数(0的出现的次数比1出现的次数多))
- POJ 3461 Oulipo(KMP统计子串出现次数)
- (DS1.5.7)POJ 2136 Vertical Histogram(以统计图的形式显示每一个字母出现的次数)
- POJ 3461 Oulipo(KMP 模板 一个串在另一个串出现的次数(可重叠|不可))
- POJ 3461 Oulipo(KMP:统计一个串出现的次数)
- 后缀数组(至少重复k次的可重叠的最长重复子串)—— POJ 3882
- 【POJ 3368】【RMQ 或者 线段树】Frequent values【求出区间内连续出现次数最多的数的次数。】