poj 3294
2016-05-05 17:42
423 查看
Life Forms
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
Sample Output
3952K 750MS G++ 2114B
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 12688 | Accepted: 3552 |
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 ? [b]二分长度把height分组,同组在不同的n/2个串里就是答案,只需记录该组中任意一个后缀的起点 SA尤其要注意开够数组,免得连接串的时候出错 [/b]
#include<cstdio> #include<cstring> #include<algorithm> #define MN 200003 using namespace std; int n,m,nm,nnm; char s1[MN]; int s[MN],a[MN]; int v[MN],sa[MN],q[MN],rank[MN],h[MN],mmh=0,len,nu[MN],st[MN]; bool w[101]; inline void gr(int x){ rank[sa[1]]=1; for (int i=2;i<=n;i++) rank[sa[i]]=(s[sa[i]]==s[sa[i-1]]&&s[sa[i]+x]==s[sa[i-1]+x])?rank[sa[i-1]]:rank[sa[i-1]]+1; for (int i=1;i<=n;i++) s[i]=rank[i]; } inline void gv(){memset(v,0,sizeof(v));for (int i=1;i<=n;i++) v[s[i]]++;for (int i=1;i<=2e5;i++)v[i]+=v[i-1];} inline void gsa(){ gv();for (int i=n;i>=1;i--) sa[v[s[i]]--]=i;gr(0); for (int i=1;i<n;i<<=1){ gv();for (int j=n;j>=1;j--) if (sa[j]>i) q[v[s[sa[j]-i]]--]=sa[j]-i; for (int j=n-i+1;j<=n;j++) q[v[s[j]]--]=j; for (int j=1;j<=n;j++) sa[j]=q[j];gr(i); if (rank[sa ]==n) return; } } inline void gh(){for (int i=1,k=0,j;i<=n;h[rank[i++]]=k) for (k?k--:0,j=sa[rank[i]-1];a[i+k]==a[j+k]&&i+k<=n&&j+k<=n;k++);} int main(){ scanf("%d",&n); while(n){ nm=0; for (int i=1;i<=n;i++){ scanf("%s",s1); m=strlen(s1); for (int j=0;j<m;j++) a[++nm]=s1[j]-'a',nu[nm]=i;a[++nm]=26+i; } nnm=n; n=nm; for (int i=1;i<=nm;i++) s[i]=a[i]; gsa();gh(); int l=0,r=n,mid,bo=1,i,j,k,mmh,pos; while(l<r){ mid=(l+r+1)>>1; for (i=1,j,k=2;i<=n;i=k++){ memset(w,0,sizeof(w));mmh=0; while (h[k]>=mid&&k<=n) k++; for (j=i;j<k;j++) if (!w[nu[sa[j]]]&&nu[sa[j]]) mmh++,w[nu[sa[j]]]=1; if (mmh*2>nnm) break; } if (i<=n) l=mid;else r=mid-1; } pos=0; for (i=1,j,k=2;i<=n;i=k++){ memset(w,0,sizeof(w));mmh=0; while (h[k]>=l&&k<=n) k++; for (j=i;j<k;j++) if (!w[nu[sa[j]]]&&nu[sa[j]]) mmh++,w[nu[sa[j]]]=1; if (mmh*2>nnm) st[++pos]=sa[i]; } if (l==0) printf("?\n");else for (int i=1;i<=pos;putchar('\n'),i++) for (int j=0;j<l;j++) putchar(a[st[i]+j]+'a'); putchar('\n'); scanf("%d",&n); } }
3952K 750MS G++ 2114B
相关文章推荐
- maven 搜索不到想从本地仓库依赖的jar包--重建本地maven仓库索引
- 用PowerShell在China Azure创建ARM虚拟机
- VS2010中如何查看DLL的导出接口
- 实现ssh和scp无密码访问
- poj 1054 The Troublesome Frog 枚举
- linux删除大文件后空间没释放的问题
- 带交互的telnet小工具,golang版
- Windows Server 2016 Technical Preview 5 X64 中文版下载地址
- 单页面结构
- 论文
- shell 中的各种括号
- Freescale imx6的android lvds屏幕驱动调试
- C++第五次实验(数组分离)
- 新函数式编程语言将简化GPU应用开发
- 学习PHP的数组总结【经验】
- ContentResolver与ContenObserver示例[当通话记录改变时删除]
- memory management in the java HotSpot Virtual Machine(中文翻译)
- JUnit4使用教程-快速入门
- SUSE Linux Enterprise 12 系统安装
- HTML&CSS——琐碎的知识点(1)