uva 11107 - Life Forms
来源:互联网 发布:自学软件测试书籍 2016 编辑:程序博客网 时间:2024/05/21 19:28
Problem C: 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.
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.
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
3abcdefgbcdefghcdefghi3xxxyyyzzz0
Output for Sample Input
bcdefgcdefgh?
这道题有点坑,首先是我无法理解more than a half。最后能AC的程序是这样算的,如果只有1个字符串,那么more than a half算1。
用(1,1)表示,然后是(2,2),(3,2),(4,3)算是看出规律了(n,n/2+1)。好吧,就这么理解吧。
这题大白上有题解,这道题关键在于记录新字符串中每个字符对应的是哪个原串,用idx数组记录,再输入数据时就可以做完这事。
然后就是二分答案,t=1时,必须特别计算。
我是先找到答案maxlen的,然后再查一遍height数组,把每个分组里原串个数>=t/2+1的输出来。
总感觉代码还是有bug,数据水了点就AC了。
代码:
#include<cstdio>#include<cstring>#include<iostream>#define Maxn 1000200using namespace std;int r[Maxn],sa[Maxn],Rank[Maxn],height[Maxn];int wa[Maxn],wb[Maxn],rs[Maxn],wv[Maxn];char str[1010];int cmp(int *r,int a,int b,int l){ return r[a]==r[b]&&r[a+l]==r[b+l];}void da(int n,int m){ int i,j,p,*x=wa,*y=wb; for(i=0;i<m;i++) rs[i]=0; for(i=0;i<n;i++) rs[x[i]=r[i]]++; for(i=1;i<m;i++) rs[i]+=rs[i-1]; for(i=n-1;i>=0;i--) sa[--rs[x[i]]]=i; for(j=1,p=1;p<n;j<<=1,m=p){ 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++) rs[i]=0; for(i=0;i<n;i++) rs[wv[i]=x[y[i]]]++; for(i=1;i<m;i++) rs[i]+=rs[i-1]; for(i=n-1;i>=0;i--) sa[--rs[wv[i]]]=y[i]; swap(x,y); for(p=1,x[sa[0]]=0,i=1;i<n;i++) x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++; }}void calheight(int n){ int i,j,k=0; for(int i=1;i<n;i++) Rank[sa[i]]=i; for(int i=1;i<n;height[Rank[i++]]=k){if(k) k--; for(j=sa[Rank[i]-1];r[i+k]==r[j+k];k++); }}int idx[Maxn];int add(int x,int cur){ for(int i=0;str[i];i++){ r[cur]=str[i]; idx[cur++]=x; } r[cur++]=125+x; return cur;}bool check(int n,int mid,int num){ int cnt=1,f[110]; memset(f,0,sizeof f); f[idx[sa[1]]]=true; for(int i=2;i<n;i++) if(height[i]<mid) {cnt=1;memset(f,0,sizeof f);f[idx[sa[i]]]=true;} else{ if(!f[idx[sa[i]]]) {cnt++;f[idx[sa[i]]]=true;} if(cnt>=num) return true; } return false;}void print(int n,int len,int num){ int cnt=1,f[110]; memset(f,0,sizeof f); f[idx[sa[1]]]=true; bool flag=true; for(int i=2;i<n;i++) if(height[i]<len) {cnt=1;flag=true;memset(f,0,sizeof f);f[idx[sa[i]]]=true;} else{ if(!f[idx[sa[i]]]) {cnt++;f[idx[sa[i]]]=true;} if(cnt>=num&&flag){ for(int j=0;j<len;j++) printf("%c",r[sa[i]+j]); puts(""); flag=false; } }}int main(){ int t,cas=0; while(scanf("%d",&t),t){ if(cas++) puts(""); int cur=1; for(int i=0;i<t;i++){ scanf("%s",str); cur=add(i,cur); } if(t==1) {printf("%s\n",str);continue;} r[0]=r[cur]=0; da(cur,250); calheight(cur); int l=0,r=cur; while(l<r){ int mid=l+r+1>>1; if(check(cur,mid,(t>>1)+1)) l=mid; else r=mid-1; } if(l==0) puts("?"); else print(cur,l,(t>>1)+1); }return 0;}
- uva 11107 - Life Forms
- UVA 11107(Life Forms-后缀数组+二分)
- UVA 11107 Life Forms 后缀数组
- UVA 11107 - Life Forms(后缀数组)
- uva 11107 - Life Forms(后缀数组)
- uva 11107 Life Forms(后缀数组)
- Uva 11107 Life Forms(后缀数组 + 二分)
- UVA 11107 Life Forms(后缀数组)
- UVA 11107 Life Forms (后缀数组 + 二份答案)
- UVA 11107 Life Forms(后缀数组 OR hash)
- UVA 11107 Life Forms(后缀数组+LCP)
- POJ 3294 (UVA 11107) Life Forms 后缀数组
- UVA 11107 Life Forms(后缀数组+二分)
- LA 11107 Life Forms
- UVa 11107 - Life Forms (后缀数组 求出现K次的串 根据height分组)
- 连续子串中出现超过一半次数的字符串 后缀数组 uva 11107 Life Forms
- Life Forms
- poj 3294 Life Forms
- [系统教程] Mac OS X 命令行中 10 组快捷键(导航、编辑)
- JAVA类加载,对象
- 我的java学习之路
- 2014家用电器设计论坛邀请
- WordPress文章列表缩略显示,文章摘要插件 WP Limit Posts Automatically导致末尾乱码——解决办法
- uva 11107 - Life Forms
- Weinre安装
- ACM--括号配对问题
- java设计模式之桥梁模式(10)
- 黑马视频学习笔记-FOUNDATION-Dictionary
- ios 根据经纬度定位城市
- 使用git checkout 指定git代码库上的指定分支
- 【线段树】POJ 3667 Hotel 区间合并
- 葡萄城旗下控件8月份重大升级