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 ...
随机推荐
- Extjs:添加查看全部按钮
var grid =new Ext.grid.GridPanel({ renderTo:'tsllb', title:'产品成本列表', selModel:csm, height:350, colum ...
- Ext:添加进度条
var myMask = new Ext.LoadMask(Ext.getBody(), {msg:"正在提交..."}); myMask.show(); myMask.hide( ...
- Ibatis组合键关联查询
在使用Ibatis时,会经常遇到关联查询,一般有两种解决方案: 使用代码进行关联查询 使用Ibatis配置文件进行关联查询 使用代码进行关联查询不作解释,本次主要是针对Ibatis配置文件进行关联查询 ...
- WebService两种调用方法
1.wsimport生成本地客户端代码 命令提示窗口执行生成命令. 格式:wsimport -s "src目录" -p “生成类所在包名” -keep “wsdl发布地址” 示例: ...
- 改变input光标颜色与输入字体颜色不同
设置input css: color #ffd600text-shadow 0px 0px 0px #bababa -webkit-text-fill-color initial input, tex ...
- Unity3D安装破解教程(以Unity5.3.4为例)(转)
Unity3D是由Unity Technologies开发的一个让玩家轻松创建诸如三维视频游戏.建筑可视化.实时三维动画等类型互动内容的多平台的综合型游戏开发工具,是一个全面整合的专业游戏引擎.目 ...
- WPF-TreeView获取文件夹目录、DataGrid获取目录下文件信息
开发一个WPF桌面应用程序.刚接触WPF编程以及C#语言,这里把一些关键的问题记录下来. 下面是实现将路径的文件夹信息绑定到TreeView及DataGrid上显示. 关键问题是C#数据绑定方式及IE ...
- vSphere笔记01~02
Vmware vsphere 虚拟化 云和大数据的底层!!!! 分类 1.开源:openstack:Linux:难(无图形化) nosqleasystack公司 2.企业版本:vsphere sdn! ...
- BGP基础【第三部】
静态路由的优点:安全稳定.缺点:配置繁琐不灵活.动态路由的优缺点则反之. BGP边界网关路由协议 路径向量(rip是距离矢量) 到达目的网段所要经过的所有as BGP选路不看度量值而参考13种路径属性 ...
- FFmpeg 常用命令收集
FFmpeg 常用命令 合并视频 ffmpeg -i "KTDS-820A_FHD.mp4" -c copy -bsf:v h264_mp4toannexb -f mpegts i ...