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 ?



【题意】

  给定n个字符串,求出现在大于n/2个字符串中的最长子串。

【分析】

  把n个串拼起来,中间插入不同的特殊字符(注意要不同的)。然后求height数组。

  二分答案,根据二分的L分组,统计组内有多少个来自不同的串即可。

  最后输出的字符串要按字典序,不过不用排序,因为求出的sa表示它已经排好序了,所以就按照顺序输入就好了。

  傻逼的我RE了很久竟然是数组大小开的混乱~~呵呵~~

代码如下:

 #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define Maxn 410
#define Maxl 4010
#define Ml 400010 int c[Ml];
int n,cl; char s[Maxl];
int p[Ml];
void init()
{
cl=;
for(int i=;i<=n;i++)
{
scanf("%s",s);
int l=strlen(s);
for(int j=;j<l;j++) c[++cl]=s[j]-'a'+,p[cl]=i;
c[++cl]=+i,p[cl]=;
}
} int sa[Ml],rk[Ml],Rs[Ml],y[Ml],wr[Ml];
void get_sa(int m)
{
memcpy(rk,c,sizeof(rk));
for(int i=;i<=m;i++) Rs[i]=;
for(int i=;i<=cl;i++) Rs[rk[i]]++;
for(int i=;i<=m;i++) Rs[i]+=Rs[i-];
for(int i=cl;i>=;i--) sa[Rs[rk[i]]--]=i; int ln=,p=;//p表示目前有多少个不一样的rk
while(p<cl)
{
int k=;
for(int i=cl-ln+;i<=cl;i++) y[++k]=i;
for(int i=;i<=cl;i++) if(sa[i]>ln) y[++k]=sa[i]-ln;
for(int i=;i<=cl;i++) wr[i]=rk[y[i]]; for(int i=;i<=m;i++) Rs[i]=;
for(int i=;i<=cl;i++) Rs[wr[i]]++;
for(int i=;i<=m;i++) Rs[i]+=Rs[i-];
for(int i=cl;i>=;i--) sa[Rs[wr[i]]--]=y[i]; for(int i=;i<=cl;i++) wr[i]=rk[i];
for(int i=cl+;i<=cl+ln;i++) wr[i]=;
p=,rk[sa[]]=;
for(int i=;i<=cl;i++)
{
if(wr[sa[i]]!=wr[sa[i-]]||wr[sa[i]+ln]!=wr[sa[i-]+ln]) p++;
rk[sa[i]]=p;
}
ln*=;m=p;
}
sa[]=rk[]=;
} int height[Ml];
void get_he()
{
int k=;
for(int i=;i<=cl;i++) if(rk[i]!=)
{
int j=sa[rk[i]-];
if(k) k--;
while(c[i+k]==c[j+k]&&i+k<=cl&&j+k<=cl) k++;
height[rk[i]]=k;
}
} bool inq[Maxn];
queue<int > q;
int ans[][Ml],now;
bool check(int x)
{
if(x==) return ;
ans[-now][]=;
int cnt=;
bool ok=;
for(int i=;i<=cl-n;i++)
{
if(!inq[p[sa[i]]])
{
q.push(p[sa[i]]);
inq[p[sa[i]]]=;
cnt++;
}
if(height[i+]<x)//new group
{
if(cnt>n/&&cnt!=) ok=,ans[-now][++ans[-now][]]=sa[i];
cnt=;
while(!q.empty()) {inq[q.front()]=;q.pop();}
}
}
if(ok) now=-now;
return ok;
} void ffind()
{
now=;
while(!q.empty()) q.pop();
memset(inq,,sizeof(inq));
int l=,r=cl;
while(l<r)
{
int mid=(l+r+)>>;
if(check(mid)) l=mid;
else r=mid-;
}
if(l==) printf("?\n");
else
{
for(int i=;i<=ans[now][];i++)
{
for(int j=;j<l;j++)
printf("%c",c[j+ans[now][i]]-+'a');
printf("\n");
}
}
} int main()
{
while()
{
scanf("%d",&n);
if(n==) break;
init();
get_sa(+n);
get_he();
ffind();
printf("\n");
}
return ;
} [POJ3294]

[POJ3294]

2016-07-18 11:04:28


												

【POJ3294】 Life Forms (后缀数组+二分)的更多相关文章

  1. POJ3294 Life Forms —— 后缀数组 最长公共子串

    题目链接:https://vjudge.net/problem/POJ-3294 Life Forms Time Limit: 5000MS   Memory Limit: 65536K Total ...

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

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

  3. POJ3294 Life Forms(后缀数组)

    引用罗穗骞论文中的话: 将n 个字符串连起来,中间用不相同的且没有出现在字符串中的字符隔开,求后缀数组.然后二分答案,用和例3 同样的方法将后缀分成若干组,判断每组的后缀是否出现在不小于k 个的原串中 ...

  4. POJ3294--Life Forms 后缀数组+二分答案 大于k个字符串的最长公共子串

                                                                              Life Forms Time Limit: 500 ...

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

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

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

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

  7. BZOJ 3230: 相似子串( RMQ + 后缀数组 + 二分 )

    二分查找求出k大串, 然后正反做后缀数组, RMQ求LCP, 时间复杂度O(NlogN+logN) -------------------------------------------------- ...

  8. BZOJ_2946_[Poi2000]公共串_后缀数组+二分答案

    BZOJ_2946_[Poi2000]公共串_后缀数组+二分答案 Description          给出几个由小写字母构成的单词,求它们最长的公共子串的长度. 任务: l        读入单 ...

  9. 【bzoj4310】跳蚤 后缀数组+二分

    题目描述 很久很久以前,森林里住着一群跳蚤.一天,跳蚤国王得到了一个神秘的字符串,它想进行研究. 首先,他会把串分成不超过 k 个子串,然后对于每个子串 S,他会从S的所有子串中选择字典序最大的那一个 ...

  10. BZOJ 1717 [USACO06DEC] Milk Patterns (后缀数组+二分)

    题目大意:求可重叠的相同子串数量至少是K的子串最长长度 洛谷传送门 依然是后缀数组+二分,先用后缀数组处理出height 每次二分出一个长度x,然后去验证,在排序的后缀串集合里,有没有连续数量多于K个 ...

随机推荐

  1. 关于XCode5打开工程闪退的一种解决方案

    今天同事遇到一个问题,是关于xcode5打开工程文件一直闪退的问题.后来查看了一下崩溃日志.有如下描述: xception Type: EXC_CRASH (SIGABRT) Exception Co ...

  2. CentOS 6.7安装Java JDK

    1.下载Java JDK 下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.htm ...

  3. 用 ISNULL(), NVL(), IFNULL() and COALESCE() 函数替换空值

    在数据库操作中,往往要对一些查询出来的空值进行替换,如函数SUM(),这个函数如果没有值会返回NULL,这是我们不希望看到的, 在MySQL中我们可以这样来写: ) ... 在SQLSERVER中我们 ...

  4. SQL Server 游标

    结果集,结果集就是select查询之后返回的所有行数据的集合. 在关系数据库中,我们对于查询的思考是面向集合的.而游标打破了这一规则,游标使得我们思考方式变为逐行进行. 正常面向集合的思维方式是: 而 ...

  5. 用bootstrap的tab插件做一个图层切换效果(感觉会误导淫们,大家当乐子看吧)

    小伙伴们啊,我JS真的是个渣渣,所以总想要偷懒,于是为了实现效果就把tab插件给改了(各位大神轻拍啊,我是小白,很纯洁滴,小心脏也很脆弱)…… 最近做的项目为了考虑以后的移动设备兼容性,所以用了Boo ...

  6. Lucida Grande字体无法正常显示冒号的解决方案

    曾经贪图Mac OSX的UI漂亮,后来查到它用的是Lucida Grande字体,所以索性将win7也改成了那种字体,结果浏览器中的中文冒号全都显示为一个奇怪的符号.后来即使将字体设置回去也无法还原. ...

  7. (转)C# 中的委托和事件

    来源:http://www.tracefact.net/CSharp-Programming/Delegates-and-Events-In-CSharp.aspx 引言 委托 和 事件在 .Net ...

  8. Javascript基础学习(1)_类型、值和变量

    1.null和undefined ①概念上区别: null是一个特殊的对象,是“非对象”,使用typeof后是object对象 undefined用未定义的值表示更深层次的“空值”,它是变量的一种取值 ...

  9. 初学HTML5系列一:简单介绍

    最近很闲,就想着学点东西,然后就瞄中了html5,以前只看过很简单的一些,这次是系统的学下,顺便也记录下.废话不多说,开始正题. 稍微介绍下html5,html5是W3C和WHATWG 合作的结果. ...

  10. Windows Azure 试用 for 世纪互联运维

    前一段时间申请由世纪互联运维的和谐版Windows Azure的邀请嘛下来,今天花费了点时间注册了一下 注册邀请函, 根据提示输入邀请码之后会收到以下邮件 中国地区可选择建立的虚拟机,SQL Serv ...