Life Forms
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 16223   Accepted: 4763

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 ?

Source

题意:
给定 n 个字符串,求出现在多于 (n/2)个字符串中的最长子串,按照字典序输出所有的解。没有就输出“?”。
代码:
//论文题,将 n 个字符串连起来,中间用不相同的且没有出现在字符串中的字符隔开,求后缀数组。然后二分答案,将后缀
//分成若干组,判断每组的后缀是否出现在不小于 k 个的原串中。这个做法的时间复杂度为 O(nlogn)。
//数组要开大一些不然re。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int MAXN=;
int sa[MAXN+],he[MAXN+],ra[MAXN+],xx[MAXN+],yy[MAXN+],buc[MAXN+];
int s[MAXN+],id[MAXN+],vis[],q[];
int len,m,top;
void get_suf()
{
int *x=xx,*y=yy;
for(int i=;i<m;i++) buc[i]=;
for(int i=;i<len;i++) buc[x[i]=s[i]]++;
for(int i=;i<m;i++) buc[i]+=buc[i-];
for(int i=len-;i>=;i--) sa[--buc[x[i]]]=i;
for(int k=;k<=len;k<<=){
int p=;
for(int i=len-;i>=len-k;i--) y[p++]=i;
for(int i=;i<len;i++) if(sa[i]>=k) y[p++]=sa[i]-k;
for(int i=;i<m;i++) buc[i]=;
for(int i=;i<len;i++) buc[x[y[i]]]++;
for(int i=;i<m;i++) buc[i]+=buc[i-];
for(int i=len-;i>=;i--) sa[--buc[x[y[i]]]]=y[i];
swap(x,y);
p=;x[sa[]]=;
for(int i=;i<len;i++){
if(y[sa[i-]]==y[sa[i]]&&y[sa[i-]+k]==y[sa[i]+k])
x[sa[i]]=p-;
else x[sa[i]]=p++;
}
if(p>=len) break;
m=p;
}
for(int i=;i<len;i++) ra[sa[i]]=i;
int k=;
for(int i=;i<len;i++){
if(ra[i]==) { he[]=; continue; }
if(k) k--;
int j=sa[ra[i]-];
while(s[i+k]==s[j+k]&&i+k<len&&j+k<len) k++;
he[ra[i]]=k;
}
}
bool solve(int mid,int n)
{
memset(vis,,sizeof(vis));
int l=,qq[],cnt=,st=-;
for(int i=;i<len;i++){
if(he[i]<mid){
if(cnt>n/&&st!=-) qq[++l]=st;
memset(vis,,sizeof(vis));
cnt=;st=-;
}else{
if(st==-) st=i-;
if(!vis[id[sa[i]]]) cnt++;
vis[id[sa[i]]]=;
if(!vis[id[sa[i-]]]) cnt++;
vis[id[sa[i-]]]=;
}
}
if(cnt>=n/&&st!=-) qq[++l]=st;
if(l){
top=l;
for(int i=;i<=l;i++) q[i]=qq[i];
return ;
}else return ;
}
int main()
{
int n;
char ch[];
while(scanf("%d",&n)&&n){
len=;
top=;
int r=,l=,ans=;
for(int i=;i<=n;i++){
scanf("%s",ch);
int tmp=strlen(ch);
r=max(r,tmp);
for(int j=;j<tmp;j++){
s[len]=ch[j]-'a';
id[len++]=i;
}
s[len]=i+;
id[len++]=;
}
m=;
get_suf();
while(l<=r){
int mid=(l+r)>>;
if(solve(mid,n)) { ans=mid;l=mid+; }
else r=mid-;
}
if(ans==){
printf("?\n\n");
continue;
}
for(int i=;i<=top;i++){
for(int j=sa[q[i]];j<=sa[q[i]]+ans-;j++) printf("%c",s[j]+'a');
printf("\n");
}
printf("\n");
}
return ;
}

poj 3294 后缀数组 多字符串中不小于 k 个字符串中的最长子串的更多相关文章

  1. poj 3294 后缀数组+二分

    题目大意: 给定n个字符串,求出现在不小于k个字符串中的最长子串 基本思路: 二分长度,统计个数,一般套路,就是这个跟说好的不一样啊,我非得开2倍才不re,真他妈不爽,先二分找出长度,然后根据长度输出 ...

  2. POJ 3294 后缀数组

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

  3. POJ-3294-Life Forms(后缀数组-不小于 k 个字符串中的最长子串)

    题意: 给定 n 个字符串,求出现在不小于 k 个字符串中的最长子串. 分析: 将 n 个字符串连起来,中间用不相同的且没有出现在字符串中的字符隔开,求后缀数组. 然后二分答案,将后缀分成若干组,判断 ...

  4. poj 3693 后缀数组 重复次数最多的连续重复子串

    Maximum repetition substring Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8669   Acc ...

  5. SPOJ PHRASES 每个字符串至少出现两次且不重叠的最长子串

    Description You are the King of Byteland. Your agents have just intercepted a batch of encrypted ene ...

  6. poj 3415 后缀数组 两个字符串中长度不小于 k 的公共子串的个数

    Common Substrings Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 11469   Accepted: 379 ...

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

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

  8. Life Forms POJ - 3294(不小于k个字符串中的最长子串)

    题意: 求不小于字符串一半长度个字符串中的最长字串 解析: 论文题例11 将n个字符串连起来,中间用不相同的且没有出现在字符串中的字符隔开, 求后缀数组, 然后二分答案变为判定性问题, 然后判断每组的 ...

  9. poj 2774 后缀数组 两个字符串的最长公共子串

    Long Long Message Time Limit: 4000MS   Memory Limit: 131072K Total Submissions: 31904   Accepted: 12 ...

随机推荐

  1. Python20-Day04

    ##########迭代器.生成器和面向过程编程########## 一.迭代器 迭代器是一个重复的过程,每次重复即一次迭代,并且每次迭代的结果都是下一次迭代的初始值: l = [1,2,3] cou ...

  2. 感谢Thunder团队

    不知不觉中,团队开发的beta版本都已经结束.开发的路上我们一起解决了很多难题,相互帮助走到了现在. 首先我想感谢组长王航.认真负责合理分配任务,使得我们每次发布都可以顺利并且按时完成.感谢胡佑蓉,李 ...

  3. 软工1816 · Beta冲刺(4/7)

    团队信息 队名:爸爸饿了 组长博客:here 作业博客:here 组员情况 组员1(组长):王彬 过去两天完成了哪些任务 推进安卓端各个接口的开发,安卓端各个接口已经基本完成 完成食堂各个平面图的绘制 ...

  4. git 提交本地文件,删除文件夹,修改文件等

    1. 下载git工具包 链接: https://git-scm.com/download/win 2. 右键打开git bash 登陆到自己的github账户 $ git config --globa ...

  5. MAVEN教程--01安装|创建|解释

    Maven是一个采用纯Java编写的开 源项目管理工具.Maven采用了一种被称之为project object model (POM)概念来管理项目,所有的项目配置信息都被定义在一个叫做POM.xm ...

  6. Scanner的例子

    package com.firstDay.one; import java.util.Scanner; public class Information { /** * @param args */ ...

  7. Unity如何判断网络状态?

    根据Application.internetReachability来判断网络状态 NetworkReachability.NotReachable 网络不可用 NetworkReachability ...

  8. JavaWeb:HttpSession(一)

    Session机制: 1).session机制采用的是在服务器端保持 HTTP 状态信息的方案 . 2).当程序需要为某个客户端的请求创建一个session时,服务器首先检查这个客户端的请求里是否包含 ...

  9. jsp 页面和 jsp标记

    一个jsp页面可由5种元素组成 html标记 变量和方法的声明 java程序片 java表达式 <%!变量和方法的声明%> 被声明的方法和变量在整个jsp页面都可以访问,为全局变量 当多个 ...

  10. 爬虫学习之-scrapy交互式命令

    scrapy shell https:///www.baidu.com  会启动爬虫请求网页 view(response) 会在浏览器打开请求到的临时文件 response.xpath("/ ...