Life Forms (poj3294 后缀数组求 不小于k个字符串中的最长子串)
| Time Limit: 5000MS | Memory Limit: 65536K | |
| Total Submissions: 8683 | Accepted: 2375 |
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 <stdio.h>
#include <math.h>
#include <vector>
#include <string.h>
using namespace std;
#define N 101000
vector<int> ai;
int a[N],c[N],d[N],e[N],sa[N],height[N],n,b[N],m,t,jilu[],bi[];
int cmp(int *r,int a,int b,int l)
{
return r[a]==r[b]&&r[a+l]==r[b+l];
}
void da()
{
int i,j,p,*x=c,*y=d,*t;
memset(b,,sizeof(b));
for(i=; i<n; i++)b[x[i]=a[i]]++;
for(i=; i<m; i++)b[i]+=b[i-];
for(i=n-; i>=; i--)sa[--b[x[i]]]=i;
for(j=,p=; p<n; j*=,m=p)
{
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<n; i++)e[i]=x[y[i]];
for(i=; i<m; i++)b[i]=;
for(i=; i<n; i++)b[e[i]]++;
for(i=; i<m; i++)b[i]+=b[i-];
for(i=n-; i>=; i--)sa[--b[e[i]]]=y[i];
for(t=x,x=y,y=t,p=,x[sa[]]=,i=; i<n; i++)
x[sa[i]]=cmp(y,sa[i-],sa[i],j)?p-:p++;
}
}
void callheight()
{
int i,j,k=;
b[]=;
for(i=; i<n; i++)b[sa[i]]=i;
for(i=; i<n-; height[b[i++]]=k)
for(k?k--:,j=sa[b[i]-]; a[i+k]==a[j+k]; k++);
}
int fun(int x)
{
int i;
for(i=; i<t; i++)
{
if(jilu[i]>=x)break;
}
return i;
}
bool check(int mid,int y)
{
int i=;
while()
{
while(i<n&&height[i]<mid)i++;
if(i==n)break;
memset(bi,,sizeof(bi));
int u=fun(sa[i-]),uu=sa[i-];
bi[u]=;
int sum=;
while(i<n&&height[i]>=mid)
{
int yu=fun(sa[i]);
if(yu==u)
{
i++;
continue;
}
else if(!bi[yu])
{
bi[yu]=;
sum++;
u=yu;
}
i++;
}
if(sum>t/)
{
if(y)
ai.push_back(uu);
else
return ;
}
}
return ;
}
void check1(int mid)
{
int i=;
while()
{
while(i<n&&height[i]<mid)i++;
if(i==n)break;
memset(bi,,sizeof(bi));
int u=fun(sa[i-]),uu=sa[i-];
bi[u]=;
int sum=;
while(i<n&&height[i]>=mid)
{
int yu=fun(sa[i]);
if(yu==u)
{
i++;
continue;
}
else if(!bi[yu])
{
bi[yu]=;
sum++;
u=yu;
}
i++;
}
if(sum>t/)ai.push_back(uu);
}
}
int main()
{
int i,j,temp,ll=;
char x;
while(scanf("%d",&t)&&t)
{
ai.clear();
if(ll)printf("\n");
ll++;
n=;
temp=;
x=getchar();
for(i=; i<t; i++)
{
while(x=getchar())
{
if(x=='\n')break;
a[n++]=x-'a'+;
}
jilu[i]=n-;
a[n++]=temp++;
}
m=;
a[n-]=;
da();
callheight();
int l=,r=;
while(l<=r)
{
int mid=(l+r)>>;
if(check(mid,))
l=mid+;
else r=mid-;
}
if(r)
{
check(r,);
for(j=; j<ai.size(); j++)
{
for(i=; i<r; i++)
{
putchar('a'+a[ai[j]+i]-);
}
printf("\n");
}
}
else
{
printf("?\n");
}
}
}
Life Forms (poj3294 后缀数组求 不小于k个字符串中的最长子串)的更多相关文章
- POJ-3294-Life Forms(后缀数组-不小于 k 个字符串中的最长子串)
题意: 给定 n 个字符串,求出现在不小于 k 个字符串中的最长子串. 分析: 将 n 个字符串连起来,中间用不相同的且没有出现在字符串中的字符隔开,求后缀数组. 然后二分答案,将后缀分成若干组,判断 ...
- poj 3294 后缀数组 多字符串中不小于 k 个字符串中的最长子串
Life Forms Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 16223 Accepted: 4763 Descr ...
- Life Forms POJ - 3294(不小于k个字符串中的最长子串)
题意: 求不小于字符串一半长度个字符串中的最长字串 解析: 论文题例11 将n个字符串连起来,中间用不相同的且没有出现在字符串中的字符隔开, 求后缀数组, 然后二分答案变为判定性问题, 然后判断每组的 ...
- 【POJ 3294】Life Forms 不小于k个字符串中的最长子串
一下午和一晚上都在刚这道题,各种错误都集齐了so sad 我的时间啊!!! 后缀数组就先做到这里吧,是在伤不起啊QAQ 出现了各种奇怪的错误,看了标算,然后乱改自己的代码,莫名其妙的改A了,后来发现用 ...
- UVa 11107 生命的形式(不小于k个字符串中的最长子串)
https://vjudge.net/problem/UVA-11107 题意:给定n个字符串,求出现在不小于n的一半个字符串的最长子串,如果有多个,则按字典序输出. 思路: 首先就是将这n个字符串连 ...
- POJ-Common Substrings(后缀数组-长度不小于 k 的公共子串的个数)
题意: 长度不小于 k 的公共子串的个数 分析: 基本思路是计算 A 的所有后缀和 B 的所有后缀之间的最长公共前缀的长度,把最长公共前缀长度不小于 k 的部分全部加起来. 先将两个字符串连起来,中间 ...
- POJ3294--Life Forms 后缀数组+二分答案 大于k个字符串的最长公共子串
Life Forms Time Limit: 500 ...
- 【poj3294-不小于k个字符串中最长公共子串】后缀数组
1.注意每两个串之间的连接符要不一样. 2.分组的时候要注意最后一组啊!又漏了! 3.开数组要考虑连接符的数量.100010是不够的至少要101000. #include<cstdio> ...
- poj 3261 二分答案+后缀数组 求至少出现k次的最长重复子序列
#include "stdio.h" #define maxn 20010 int wa[maxn],wb[maxn],wv[maxn],ws[maxn]; int rank[ma ...
随机推荐
- Ubuntu部署可视化爬虫Portia2.0环境
部署portia环境官方文档给出的方法太过简单,对于初学者来说是很难根据那一两行字成功部署portia环境的.对于部署portia这只可爱的爬虫的过程还是有很多坑的,主要写一篇portia2.0版本的 ...
- 编译安装dropbear
author:JevonWei 版权声明:原创作品 dropbear也可实现ssh远程登录的作业 1. 安装开发包组 yum -y groupinstall "Development Too ...
- [js高手之路]深入浅出webpack系列2-配置文件webpack.config.js详解
接着上文,重新在webpack文件夹下面新建一个项目文件夹demo2,然后用npm init --yes初始化项目的package.json配置文件,然后安装webpack( npm install ...
- java并发之原子性
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp17 1.原子性操作: 不能被线程调度机制中断的操作:--对原子性变量的赋值 ...
- [转]Java se 7 最新特性研究(一)
详见: http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp81 从2006到现在等待了多年的jdk7终于发布了.这里将对它的一些 ...
- 探索JSP中的 "9大内置对象!"
1.什么是JSP内置对象? jsp内置对象就是Web容器创建的一组对象,我们都知道Tomcat可以看成是一种Web容器,所以我们可以知道所谓的内置对象Tomcat创建的,使用内置对象时可以不适用new ...
- web切图的几个快捷键及总结
自由的控制视图系列 自由的浏览图片:按住空格后,鼠标拖动 自由的缩放图片:按住alt+滚轮 切换到实际像素(100%):Ctrl + 1 工作区类型切换:Tab 控制内容系列 通过画面上点选一个图 ...
- (转)C语言malloc()与free()的使用
如何使用 malloc 函数 本文为转载内容,原文地址请点击 不要莫名其妙,其实上面这段小小的对话,就是malloc的使用过程.malloc是一个函数,专门用来从堆上分配内存.使用malloc函数需要 ...
- 201521123100 《Java程序设计》 第7周学习总结
1. 本周学习总结 以你喜欢的方式(思维导图或其他)归纳总结集合相关内容. 参考资料: XMind 2. 书面作业 1.ArrayList代码分析 1.1 解释ArrayList的contains源代 ...
- 201521123100 《Java程序设计》第6周学习总结
1. 本周学习总结 1.1 面向对象学习暂告一段落,请使用思维导图,以封装.继承.多态为核心概念画一张思维导图,对面向对象思想进行一个总结. 注1:关键词与内容不求多,但概念之间的联系要清晰,内容覆盖 ...