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 ...
随机推荐
- Bootstrap-table事件使用
HTML <div class="alert alert-danger" id="eventInfo"></div> <table ...
- SSH2项目网上书店系统手把手教学_Struts2+Spring+Hibernate整合开发
一 序言 鉴于目前J2EE的火热程度,SSH2是每个学生毕业前都必须掌握的一门技术,所以在这里我就使用SSH2技术做一个小型项目,和大家一起学习. SSH2技术的基础概论就不再提了,直接说特点吧. 1 ...
- MySQL视图view/存储过程和函数的使用
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px "Helvetica Neue"; color: #454545 } p. ...
- Windows系统安装Azure CLI
本文将介绍在Windos系统下如下安装CLI 1.打开Azure官方链接:https://www.azure.cn/downloads/ 2.按照向导进行安装 3.打开Windows Powershe ...
- 【Java IO流】字节流和字符流详解
字节流和字符流 对于文件必然有读和写的操作,读和写就对应了输入和输出流,流又分成字节和字符流. 1.从对文件的操作来讲,有读和写的操作——也就是输入和输出. 2.从流的流向来讲,有输入和输出之分. 3 ...
- windows系统,优化C盘空间的方法
C盘在使用过程中,内容会越来越多,剩余空间越来越小.如何清理出更多空间呢?以windows7为例 转载请保留 http://www.cnblogs.com/lion-zheng/ cleanmgr w ...
- css预处理器less和scss之less介绍(一)
第一次发的标题有误,重发一遍,抱歉了 一.less基础语法 1.声明变量:@变量名:变量值 使用变量:@变量名 例如 @color : #ff0000; @length : 100px; #div1{ ...
- 软件工程(GZSD2015)第三次作业
时间: 2015/4/17-2015/4/23 基本要求: 在之前编写的四则运算程序基础之上做如下改进: 请参照教材Page57:4.2-4.3节中内容,修改原程序,使之符合 "代码风格和设 ...
- 个人作业二——英语学习APP 案例分析
英语学习APP的案例分析 我们生活中很多时候要和软件打交道,大家上课开小差时候玩的手机游戏,买火车票的网站,互相联系用的微信.QQ,等等都是软件,都很值得分析.你为何成为它们的用户?它们的团队做对了什 ...
- 201521123001《Java程序设计》第6周学习总结
1. 本周学习总结 1.1 面向对象学习暂告一段落,请使用思维导图,以封装.继承.多态为核心概念画一张思维导图,对面向对象思想进行一个总结. 注1:关键词与内容不求多,但概念之间的联系要清晰,内容覆盖 ...