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. iOS 静态库 与 demo 联合调试

    在修复bug或者开发静态库需要调试,这个时候需要把工程中的.framework和资源bundle文件都替换为静态库原工程文件 首先需要确保静态库工程文件没有打开,Xcode不允许在两个地方同时打开同一 ...

  2. Scrum立会报告+燃尽图(Final阶段第五次)

    此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2484 项目地址:https://coding.net/u/wuyy694 ...

  3. D.王者荣耀交流协会——PSP Daily(测评人:贾男男)

    D.王者荣耀交流协会——PSP Daily(测评人:贾男男) 一.基于NABCD评论作品,及改进建议 每个小组评论其他小组beta发布的作品.1.根据(不限于)NABCD评论作品的选题;2.评论作品对 ...

  4. Daily Scrum (2015/11/3)

    今天我们的爬虫能在pc上成功运行并且把所爬取的数据存到服务器上了!我们已经搭建好数据库,把相关信息存到数据库中,并把数据存到D盘里共享给数据处理小组使用. 成员 今日工作 时间 明日工作 符美潇 完成 ...

  5. Task 3 求最大数组和

    题目:返回一个整数数组中最大子数组的和. (要求:输入一个整形数组,数组里有正数也有负数. 数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和. 求所有子数组的和的最大值.要求时间复杂度为 ...

  6. vs2013 CodeLens

    那东西叫 CodeLens  只有VS2013 旗舰版 (update 2及以上) 才可以用,高级版 专业版都没有.如何打开CodeLens呢?在VS菜单栏 >> 工具 >> ...

  7. Linux手动添加系统环境共享库路径

    1.在以下目录 #/etc/ld.so.conf.d 添加相应的xxx.conf 2.写入所要共享库的路径 如:/usr/lib/ 3.导入共享库配置 $ldconfig

  8. 论文《Network in Network》笔记

    论文:Lin M, Chen Q, Yan S. Network In Network[J]. Computer Science, 2013. 参考:关于CNN中1×1卷积核和Network in N ...

  9. vue.js 中slot 用处大(转载)

    什么是组件? 组件(Component)是 Vue.js 最强大的功能之一.组件可以扩展 HTML 元素,封装可重用的代码.在较高层面上,组件是自定义元素,Vue.js 的编译器为它添加特殊功能.在有 ...

  10. ie8 ajaxSubmit 上传文件提示下载

    转载 解决ie下ajaxsubmit上传文件提示下载文件问题 主要是应为放回类型为json,返回text/html