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 ... 
随机推荐
- CSS边框外的小三角形+阴影效果的实现。
			...虽然是一个很小的问题,但其实还是挺实用的. 实现一个边框外的角. 用纯CSS来写. 一开始实现的效果是这个样子的. 但是这个效果没有办法给他附带阴影,所以换了一种写法.实现成了这个样子 想要实现 ... 
- switch case异常处理机制
			public class T3{ public static void main(String[] args) { try{ String kc=""; System.out.pr ... 
- jenkins到底如何拉取代码 如何部署的
			tips:jenkins通过配置,将之前编译.打包.上传.部署到Tomcat中的过程交由jenkins,jenkins通过指定的代码地址url,将代码拉取到其jenkins的安装位置,进行编译.打包和 ... 
- pycharm激活
			刚刚下载了2017.1版本专业版的pycharm,作为一个天朝开发者,自然是去找注册码了.转悠了一圈,那些注册码都已经失效了.看到一个有效的方法:把http://elporfirio.com:1017 ... 
- adb 安装apk 报错:Failure [INSTALL_FAILED_ALREADY_EXISTS]
			遇到INSTALL_FAILED_ALREADY_EXISTS问题,直接通过adb install -r xxx.apk命令安装apk即可 
- Android项目实战(三十四):蓝牙4.0 BLE 多设备连接
			最近项目有个需求,手机设备连接多个蓝牙4.0 设备 并获取这些设备的数据. 查询了很多资料终于实现,现进行总结. ------------------------------------------- ... 
- P问题、NP问题、NPC问题
			看师兄们的论文经常说一句这是个NP难问题,所以采用另外一种方法来代替(比如凸松弛,把l0范数的问题松弛为l1范数的问题来求解).然后搜索了相关知识,也还是没看太懂,把一些理论知识先贴上来,希望以后再接 ... 
- 浅谈如何用Java操作MongoDB
			NoSQL数据库因其可扩展性使其变得越来越流行,利用NoSQL数据库可以给你带来更多的好处,MongoDB是一个用C++编写的可度可扩展性的开源NoSQL数据库.本文主要讲述如何使用Java操作Mon ... 
- ReentrantLock和synchronized的性能对比
			详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytpo8 ReentrantLock和内部锁的性能对比 Reentran ... 
- CCIE-MPLS VPN-实验手册(上卷)
			看完了看完了看完了,豪爽豪爽豪爽,一个月了,写得挺棒.总共14个mpls vpn的实验,为留下学习的痕迹,原封不动献出. CCIE实验手册 (路由部分-MPLSVPN基础篇) [CCIE] JUST ... 
