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

3
abcdefg
bcdefgh
cdefghi
3
xxx
yyy
zzz
0

Sample Output

bcdefg
cdefgh ?

  注意一些细节就好。

 #include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn=;
char s[maxn];
int r[maxn],Wa[maxn],Wb[maxn],Wv[maxn],Ws[maxn];
int rank[maxn],lcp[maxn],belong[maxn],sa[maxn];
bool cmp(int *p,int i,int j,int l){
return p[i]==p[j]&&p[i+l]==p[j+l];
}
void DA(int n,int m){
int i,j,p,*x=Wa,*y=Wb,*t;
for(i=;i<m;i++)Ws[i]=;
for(i=;i<n;i++)++Ws[x[i]=r[i]];
for(i=;i<m;i++)Ws[i]+=Ws[i-];
for(i=n-;i>=;i--)sa[--Ws[x[i]]]=i; for(j=,p=;p<n;m=p,j<<=){
for(p=,i=n-j;i<n;i++)y[p++]=i;
for(i=;i<n;i++)
if(sa[i]>=j)
y[p++]=sa[i]-j;
for(i=;i<m;i++)Ws[i]=;
for(i=;i<n;i++)++Ws[Wv[i]=x[y[i]]];
for(i=;i<m;i++)Ws[i]+=Ws[i-];
for(i=n-;i>=;i--)
sa[--Ws[Wv[i]]]=y[i];
for(t=x,x=y,y=t,x[sa[]]=,i=,p=;i<n;i++)
x[sa[i]]=cmp(y,sa[i],sa[i-],j)?p-:p++;
}
} void LCP(int n){
int i,j,k=;
for(i=;i<=n;i++)rank[sa[i]]=i;
for(i=;i<n;lcp[rank[i++]]=k)
for(k?k--:k,j=sa[rank[i]-];r[i+k]==r[j+k];k++);
}
int tot,tim,vis[maxn];
bool Judge(int n,int x,int g){
int tmp=;++tim;
for(int i=;i<=n;i++){
if(lcp[i]<x)
tmp=,++tim;
else{
if(vis[belong[sa[i]]]!=tim)
tmp++,vis[belong[sa[i]]]=tim;
if(vis[belong[sa[i-]]]!=tim)
tmp++,vis[belong[sa[i-]]]=tim;
if(tmp>g)return true;
}
}
return false;
} void Solve(int n,int x,int g){
int tmp=,tag=;++tim;
for(int i=;i<=n;i++){
if(lcp[i]<x)
tmp=,++tim,tag=;
else{
if(vis[belong[sa[i]]]!=tim)
tmp++,vis[belong[sa[i]]]=tim;
if(vis[belong[sa[i-]]]!=tim)
tmp++,vis[belong[sa[i-]]]=tim;
if(tmp>g&&!tag){
for(int j=;j<x;j++)
printf("%c",r[sa[i-]+j]);
printf("\n");
tag=;
}
}
}
return;
} int main(){
while(~scanf("%d",&tot)&&tot){ int len=,lo=,hi=;
for(int i=;i<=tot;i++){
scanf("%s",s);
for(int j=;s[j];j++){
belong[len]=i;
r[len++]=s[j];
if(!s[j+])hi=min(hi,j+);
}
belong[len]=+i;
r[len++]='z'+i;
}
r[len]=;
DA(len+,);
LCP(len); while(lo<=hi){
int mid=(lo+hi)>>;
if(Judge(len,mid,tot/))lo=mid+;
else hi=mid-;
}
if(hi>=)
Solve(len,hi,tot/);
else
printf("?\n");
printf("\n");
}
return ;
}

字符串(后缀数组):POJ 3294 Life Forms的更多相关文章

  1. Poj 3294 Life Forms (后缀数组 + 二分 + Hash)

    题目链接: Poj 3294 Life Forms 题目描述: 有n个文本串,问在一半以上的文本串出现过的最长连续子串? 解题思路: 可以把文本串用没有出现过的不同字符连起来,然后求新文本串的heig ...

  2. POJ 3294 Life Forms(后缀数组+二分答案)

    [题目链接] http://poj.org/problem?id=3294 [题目大意] 求出在至少在一半字符串中出现的最长子串. 如果有多个符合的答案,请按照字典序输出. [题解] 将所有的字符串通 ...

  3. POJ 3294 Life Forms [最长公共子串加强版 后缀数组 && 二分]

    题目:http://poj.org/problem?id=3294 Life Forms Time Limit: 5000MS   Memory Limit: 65536K Total Submiss ...

  4. 后缀数组 POJ 3581 Sequence

    题目链接 题意:把n个数字(A1比其他数字都大)的序列分成三段,每段分别反转,问字典序最小的序列. 分析:因为A1比其他数字都大,所以反转后第一段结尾是很大的数,相当是天然的分割线,第一段可以单独考虑 ...

  5. Bzoj4556: [Tjoi2016&Heoi2016]字符串 后缀数组

    4556: [Tjoi2016&Heoi2016]字符串 Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 169  Solved: 87[Sub ...

  6. 【BZOJ 3473】 字符串 (后缀数组+RMQ+二分 | 广义SAM)

    3473: 字符串 Description 给定n个字符串,询问每个字符串有多少子串(不包括空串)是所有n个字符串中至少k个字符串的子串? Input 第一行两个整数n,k. 接下来n行每行一个字符串 ...

  7. poj 3294 Life Forms

    后缀数组的题目,把后缀连接起来,这个还是先二分答案,然后选取一段连续的height值,判断这些height代表的后缀有没有覆盖一半以上的字符串. 得出答案的长度之后还要在枚举连续的heigh,判断有没 ...

  8. BZOJ 3277: 串/ BZOJ 3473: 字符串 ( 后缀数组 + RMQ + 二分 )

    CF原题(http://codeforces.com/blog/entry/4849, 204E), CF的解法是O(Nlog^2N)的..记某个字符串以第i位开头的字符串对答案的贡献f(i), 那么 ...

  9. BZOJ3473:字符串(后缀数组,主席树,二分,ST表)

    Description 给定n个字符串,询问每个字符串有多少子串(不包括空串)是所有n个字符串中至少k个字符串的子串? Input 第一行两个整数n,k. 接下来n行每行一个字符串. Output 一 ...

  10. 【BZOJ-4556】字符串 后缀数组+二分+主席树 / 后缀自动机+线段树合并+二分

    4556: [Tjoi2016&Heoi2016]字符串 Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 657  Solved: 274[Su ...

随机推荐

  1. web前端开发浏览器兼容性 - 持续更新

    浏览器兼容性问题又被称为网页或网站兼容性问题:不同浏览器内核及所支持的html等网页语言标准不同,不同客户端环境(如分辨率不同)造成实际显示效果未能达到预期理想效果 首先我们来看一下目前市面上常见的一 ...

  2. 比较好的自学IT的网站

    其实这是我在知乎的一个回答,由于收藏人数众多,我想也许对有些初学者有用,故同步到Blog.此文章和知乎答案将不定期同步更新(知乎答案传送门). 入门与进阶: 学堂在线-最大的中文慕课(MOOC)平台学 ...

  3. Http,Https (SSL)的Url绝对路径,相对路径解决方案Security Switch 4.2 英文帮助文档 分类: ASP.NET 2014-10-28 10:50 147人阅读 评论(1) 收藏

    Security Switch 4.2 =================== Security Switch enables various ASP.NET applications to auto ...

  4. C#重载重写

    overload:重载指的是同一个类中有两个或多个名字相同但是参数不同的方法,(注:返回值不能区别函数是否重载),重载没有关键字.override:过载也称重写是指子类对父类中虚函数或抽象函数的“覆盖 ...

  5. angularJS function

    angular.bootstrap 启动Angular angular.element 相当于轻量的JQuery 使用方法: angular.element('#qq'); angular.eleme ...

  6. java对象与xml相互转换 ---- xstream

    XStream是一个Java对象和XML相互转换的工具,很好很强大.提供了所有的基础类型.数组.集合等类型直接转换的支持. XStream中的核心类就是XStream类,一般来说,熟悉这个类基本就够用 ...

  7. jdbc - 连接数据库的url

    MySql: driver:com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/database_name SQL Server 2008: ...

  8. angularjs modal模态框----创建可拖动的指令

    //最近项目中需要将angular-ui-bootstrap中用到的弹出框,使之可拖动,由于源文件中没有实现,需要自己实现指令,以下即为该指令,亲测可以实现..directive('draggable ...

  9. 受限玻尔兹曼机(RBM)

    能量模型 RBM用到了能量模型. 简单的概括一下能量模型.假设一个孤立系统(总能量$E$一定,粒子个数$N$一定),温度恒定为1,每个粒子有$m$个可能的状态,每个状态对应一个能量$e_i$.那么,在 ...

  10. vs里 .sln和.suo 文件

    Net解决方案下 .sln文件和.suo文件的解释:When a Web site is created, a solution file (.sln) and a hidden solution u ...