Life Forms
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 12688   Accepted: 3552

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 ? 二分长度把height分组,同组在不同的n/2个串里就是答案,只需记录该组中任意一个后缀的起点
SA尤其要注意开够数组,免得连接串的时候出错
#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=,len,nu[MN],st[MN];
bool w[];
inline void gr(int x){
rank[sa[]]=;
for (int i=;i<=n;i++) rank[sa[i]]=(s[sa[i]]==s[sa[i-]]&&s[sa[i]+x]==s[sa[i-]+x])?rank[sa[i-]]:rank[sa[i-]]+;
for (int i=;i<=n;i++) s[i]=rank[i];
}
inline void gv(){memset(v,,sizeof(v));for (int i=;i<=n;i++) v[s[i]]++;for (int i=;i<=2e5;i++)v[i]+=v[i-];}
inline void gsa(){
gv();for (int i=n;i>=;i--) sa[v[s[i]]--]=i;gr();
for (int i=;i<n;i<<=){
gv();for (int j=n;j>=;j--) if (sa[j]>i) q[v[s[sa[j]-i]]--]=sa[j]-i;
for (int j=n-i+;j<=n;j++) q[v[s[j]]--]=j;
for (int j=;j<=n;j++) sa[j]=q[j];gr(i);
if (rank[sa[n]]==n) return;
}
}
inline void gh(){for (int i=,k=,j;i<=n;h[rank[i++]]=k) for (k?k--:,j=sa[rank[i]-];a[i+k]==a[j+k]&&i+k<=n&&j+k<=n;k++);}
int main(){
scanf("%d",&n);
while(n){
nm=;
for (int i=;i<=n;i++){
scanf("%s",s1);
m=strlen(s1);
for (int j=;j<m;j++) a[++nm]=s1[j]-'a',nu[nm]=i;a[++nm]=+i;
}
nnm=n;
n=nm;
for (int i=;i<=nm;i++) s[i]=a[i];
gsa();gh();
int l=,r=n,mid,bo=,i,j,k,mmh,pos;
while(l<r){
mid=(l+r+)>>;
for (i=,j,k=;i<=n;i=k++){
memset(w,,sizeof(w));mmh=;
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]]]=;
if (mmh*>nnm) break;
}
if (i<=n) l=mid;else r=mid-;
}
pos=;
for (i=,j,k=;i<=n;i=k++){
memset(w,,sizeof(w));mmh=;
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]]]=;
if (mmh*>nnm) st[++pos]=sa[i];
}
if (l==) printf("?\n");else
for (int i=;i<=pos;putchar('\n'),i++)
for (int j=;j<l;j++) putchar(a[st[i]+j]+'a');
putchar('\n');
scanf("%d",&n);
}
}

3952K 750MS G++ 2114B


poj 3294的更多相关文章

  1. POJ 3294 n个串中至少一半的串共享的最长公共子串

    Life Forms Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 12484   Accepted: 3502 Descr ...

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

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

  3. POJ - 3294~Relevant Phrases of Annihilation SPOJ - PHRASES~Substrings POJ - 1226~POJ - 3450 ~ POJ - 3080 (后缀数组求解多个串的公共字串问题)

    多个字符串的相关问题 这类问题的一个常用做法是,先将所有的字符串连接起来, 然后求后缀数组 和 height 数组,再利用 height 数组进行求解. 这中间可能需要二分答案. POJ - 3294 ...

  4. POJ 3294 后缀数组

    题目链接:http://poj.org/problem?id=3294 题意:给定n个字符串,求一个最长子串要求在超过一半的字符串中出现过. 如果多解按字典序输出 思路:根据<<后缀数组— ...

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

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

  6. poj 3294 Life Forms - 后缀数组 - 二分答案

    题目传送门 传送门I 传送门II 题目大意 给定$n$个串,询问所有出现在严格大于$\frac{n}{2}$个串的最长串.不存在输出'?' 用奇怪的字符把它们连接起来.然后求sa,hei,二分答案,按 ...

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

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

  8. POJ 3294 出现在至少K个字符串中的子串

    在掌握POJ 2774(两个串求最长公共子串)以及对Height数组分组后,本题还是容易想出思路的. 首先用字符集外的不同字符连接所有串,这是为了防止两个后缀在比较时超过某个字符串的分界.二分子串的长 ...

  9. POJ 3294 Life Forms 后缀数组+二分 求至少k个字符串中包含的最长子串

    Life Forms   Description You may have wondered why most extraterrestrial life forms resemble humans, ...

  10. POJ 3294 二分找超过一半字符串中存在的子串

    题目大意: 给定n个字符串,求出现在不小于k/2个字符串中的最长子串. 二分找对应子串长度的答案,将所有字符串链接成一个长字符串求后缀数组,记录每一个位置本属于第几个字符串,利用height查询的时候 ...

随机推荐

  1. 浅谈Unix I/O模型

    关于I/O模型的文章比较多,参考多篇后理解上仍然不太满意,终需自己整理一次,也是编写高吞吐量高性能网络接口模块的基础.这里所说的主要针对网络I/O,近几年面对越来越大的用户请求量,如何优化这些步骤直接 ...

  2. C图形库Easyx的使用

    学习Eaxy X图形库后我的成果: 花了一周时间做出并完善了Flappy Bird,目前功能如下: 1. 背景的显示 2. 加入小鸟image 3. 小鸟自由下落,按空格键/鼠标右键后上升 4. 加入 ...

  3. C#设计模式之二十二备忘录模式(Memeto Pattern)【行为型】

    一.引言   今天我们开始讲"行为型"设计模式的第十个模式,该模式是[备忘录模式],英文名称是:Memento Pattern.按老规矩,先从名称上来看看这个模式,个人的最初理解就 ...

  4. sql server 2012 新知识-序列

    今天聊一聊sql 2012 上的新功能-----序列 按我的理解,它就是为了实现全局性的唯一标识,按sql server 以前的版本,想对一张表标识很简单,比如identity,但如果要对某几张有业务 ...

  5. CentOS 7.x上gitlab搭建教程(https可用,邮件可用)

    目录 知识要求 搭建感想 搭建过程 参考 知识要求: nginx基础知识 搭建感想 注:以下是我搭建gitlab时的思考,需要nginx的基础知识,Docker的基础知识才容易理解,与下面的搭建过程是 ...

  6. Nodejs的运行原理-架构篇

    前言 本来是想只做一个Nodejs运行原理-科普篇,但是收到了不少私信,要我多分享一些更进阶,更详细的内容,所以我会在接下来的两个月里继续更新Nodejs运行原理. PS:此系列只做Nodejs的运行 ...

  7. 使用Word进行文档修订版本的比较

    项目经理在实际的工作过程中,比如要写文档方案,就需要对文档的修订版本进行管理和控制.在以前的工作中,笔者使用的是UltraEdit这个软件工具中的Ultra Compare这个子工具来进行的文档版本的 ...

  8. 5、员工上班时间的问题 - CEO之公司管理经验谈

    员工上班时间一般是根据公司的规章制度来制定的.当然,在不同的地点也有不同的做法.比如北京.上海.广州.深圳这些重点的大点的城市,加班的时间就相对比较多一些.但是按照笔者的想法,一般是一天7-8小时工作 ...

  9. Eclipse项目分组管理

    对于eclipse相信对于一个java开发人员,一定不陌生.eclipse可以通过工作空间(Workspace)将不同的项目进行分开管理,相信这一点大家一定很熟悉,用过idea的小伙伴,一定发现了,i ...

  10. 怎么制作html5网站页面让它适应电脑和手机的尺寸

    https://zhidao.baidu.com/question/918130826792192539.html 用以下代码开头:<!DOCTYPE HTML><html>& ...