POJ3294-Life Forms(后缀数组)
来源:互联网 发布:淘宝卖东西侵权怎么办 编辑:程序博客网 时间:2024/05/19 18:13
POJ3294-Life Forms(后缀数组)
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
?
题意:
找出出现在多于n/2个字符串中的最长子串
求出后缀数组,二分答案,然后吧height数组以k分组,看在不同串出现次数是否多于n/2
AC代码:
#include<iostream>#include<algorithm>#include<cstdlib>#include<cstdio>#include<cstring>#include<vector>#include<cmath>#include<complex>#include<queue>#include <map>#define T 111111using namespace std;char s[T];int a[105];int t1[T],t2[T],cc[T],x[T],sa[T],rank[T],height[T];int len;int vis[105];map<int,int> mp;bool cmp(int *y,int a,int b,int k){ int a1=y[a]; int b1=y[b]; int a2=a+k>=len ? -1:y[a+k]; int b2=b+k>=len ? -1:y[b+k]; return a1==b1 && a2==b2;}int make_sa(){ int *x=t1,*y=t2; int m=255; for(int i=0; i<m; i++) cc[i]=0; for(int i=0; i<len; i++) ++cc[x[i]=s[i]]; for(int i=1; i<m; i++) cc[i]+=cc[i-1]; for(int i=len-1; i>=0; i--) sa[--cc[x[i]]]=i; for(int k=1; k<=len; k<<=1) { int p=0; for(int i=len-k; i<len; 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++) cc[i]=0; for(int i=0; i<len; i++) ++cc[x[y[i]]]; for(int i=1; i<m; i++) cc[i]+=cc[i-1]; for(int i=len-1; i>=0; i--) sa[--cc[x[y[i]]]]=y[i]; swap(x,y); m=1; x[sa[0]]=0; for(int i=1; i<len; i++) x[sa[i]]=cmp(y,sa[i],sa[i-1],k) ? m-1:m++; if( m>=len ) break; }}void make_height(){ for(int i=0; i<len; i++) rank[sa[i]]=i; height[0]=0; int k=0; for(int i=0; i<len; i++) { if(!rank[i]) continue; int j=sa[rank[i]-1]; if(k) k--; while(s[i+k]==s[j+k]) k++; height[rank[i]]=k; }}bool check(int k,int n){ int sum=1,flag=0; memset(vis,0,sizeof(vis)); for(int i=0;i<len;i++){ int z=sa[i]; map<int,int>::iterator itea; itea=mp.lower_bound(z); int y=itea->second; int lst=itea->first; if(height[i]<k||s[sa[i]]=='$'||sa[i]+k>lst){ sum=1; memset(vis,0,sizeof(vis)); vis[y]++; continue; } if(!vis[y]){ vis[y]++; sum++; } if(sum>n/2) return true; } return false;}void print(int k,int n){ int sum=1,flag=0; memset(vis,0,sizeof(vis)); for(int i=0;i<len;i++){ int z=sa[i]; map<int,int>::iterator itea; itea=mp.lower_bound(z); int y=itea->second; int lst=itea->first; if(height[i]<k||s[sa[i]]=='$'||sa[i]+k>lst){ sum=1; memset(vis,0,sizeof(vis)); vis[y]++; continue; } if(!vis[y]){ vis[y]++; sum++; } if(sum>n/2){ for(int j=0;j<k;j++){ printf("%c",s[sa[i]+j]); } printf("\n"); for(i;height[i]>=k&&i<len;i++); i--; } }}int main(){ int n; while(scanf("%d",&n)!=EOF&&n) { int m=0,maxn=0; mp.clear(); for(int i=0;i<n;i++){ scanf("%s",s+m); int y=strlen(s+m); maxn=max(maxn,y); s[m+y]='$'; m+=y+1; mp[m-1]=i; } len=m-1; s[m]='\0'; make_sa(); make_height(); int l=0,r=maxn+1; while(l!=r){ int mid=(l+r)/2; if(check(mid,n)) l=mid+1; else r=mid; } if(r-1){ print(r-1,n); } else printf("?\n"); cout<<"\n"; } return 0;}
- poj3294 Life Forms(后缀数组)
- [POJ3294]Life Forms(后缀数组+二分)
- POJ3294-Life Forms(后缀数组)
- POJ3294:Life Forms(后缀数组)
- 后缀数组 - poj3294 Life Forms
- poj3294 Life Forms - 后缀数组
- POJ3294 Life Forms 【后缀数组】
- 【POJ3294】Life Forms【后缀数组】【二分】
- poj3294 Life Forms(后缀数组+二分答案)
- POJ3294——Life Forms 后缀数组
- POJ3294---Life Forms(后缀数组,二分+给后缀分组)
- Life Forms poj3294
- POJ3294--Life Forms
- poj3294 Life Forms
- poj3294 Life Forms
- POJ3294-Life Forms
- Poj3294 Life Forms
- poj 3294 Life Forms(后缀数组)
- 寻找宁静的心情、真挚&真诚的爱情和共同努力的幸福生活!
- mybatis和spring整合框架
- 《Java虚拟机》学习简记
- mysql索引优化
- es6 字符扩展
- POJ3294-Life Forms(后缀数组)
- mysql 5.7.17新版本的安装失败记录
- 在action中将字符串、对象、list集合保存到值栈中,在jsp页面中获取的方法
- 从java的运行原理理解java的反射机制
- 1.新手关于linux的初步认识
- java日志框架学习
- 三目运算符与不同类型
- 【开源】golang高性能分布式游戏服务器框架-mqant
- Android 编译错误::app:transformClassesWithPreJackPackagedLibrariesForDebug