poj 3294 后缀数组 多字符串中不小于 k 个字符串中的最长子串
| Time Limit: 5000MS | Memory Limit: 65536K | |
| Total Submissions: 16223 | Accepted: 4763 |
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 ?
Source
//论文题,将 n 个字符串连起来,中间用不相同的且没有出现在字符串中的字符隔开,求后缀数组。然后二分答案,将后缀
//分成若干组,判断每组的后缀是否出现在不小于 k 个的原串中。这个做法的时间复杂度为 O(nlogn)。
//数组要开大一些不然re。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int MAXN=;
int sa[MAXN+],he[MAXN+],ra[MAXN+],xx[MAXN+],yy[MAXN+],buc[MAXN+];
int s[MAXN+],id[MAXN+],vis[],q[];
int len,m,top;
void get_suf()
{
int *x=xx,*y=yy;
for(int i=;i<m;i++) buc[i]=;
for(int i=;i<len;i++) buc[x[i]=s[i]]++;
for(int i=;i<m;i++) buc[i]+=buc[i-];
for(int i=len-;i>=;i--) sa[--buc[x[i]]]=i;
for(int k=;k<=len;k<<=){
int p=;
for(int i=len-;i>=len-k;i--) y[p++]=i;
for(int i=;i<len;i++) if(sa[i]>=k) y[p++]=sa[i]-k;
for(int i=;i<m;i++) buc[i]=;
for(int i=;i<len;i++) buc[x[y[i]]]++;
for(int i=;i<m;i++) buc[i]+=buc[i-];
for(int i=len-;i>=;i--) sa[--buc[x[y[i]]]]=y[i];
swap(x,y);
p=;x[sa[]]=;
for(int i=;i<len;i++){
if(y[sa[i-]]==y[sa[i]]&&y[sa[i-]+k]==y[sa[i]+k])
x[sa[i]]=p-;
else x[sa[i]]=p++;
}
if(p>=len) break;
m=p;
}
for(int i=;i<len;i++) ra[sa[i]]=i;
int k=;
for(int i=;i<len;i++){
if(ra[i]==) { he[]=; continue; }
if(k) k--;
int j=sa[ra[i]-];
while(s[i+k]==s[j+k]&&i+k<len&&j+k<len) k++;
he[ra[i]]=k;
}
}
bool solve(int mid,int n)
{
memset(vis,,sizeof(vis));
int l=,qq[],cnt=,st=-;
for(int i=;i<len;i++){
if(he[i]<mid){
if(cnt>n/&&st!=-) qq[++l]=st;
memset(vis,,sizeof(vis));
cnt=;st=-;
}else{
if(st==-) st=i-;
if(!vis[id[sa[i]]]) cnt++;
vis[id[sa[i]]]=;
if(!vis[id[sa[i-]]]) cnt++;
vis[id[sa[i-]]]=;
}
}
if(cnt>=n/&&st!=-) qq[++l]=st;
if(l){
top=l;
for(int i=;i<=l;i++) q[i]=qq[i];
return ;
}else return ;
}
int main()
{
int n;
char ch[];
while(scanf("%d",&n)&&n){
len=;
top=;
int r=,l=,ans=;
for(int i=;i<=n;i++){
scanf("%s",ch);
int tmp=strlen(ch);
r=max(r,tmp);
for(int j=;j<tmp;j++){
s[len]=ch[j]-'a';
id[len++]=i;
}
s[len]=i+;
id[len++]=;
}
m=;
get_suf();
while(l<=r){
int mid=(l+r)>>;
if(solve(mid,n)) { ans=mid;l=mid+; }
else r=mid-;
}
if(ans==){
printf("?\n\n");
continue;
}
for(int i=;i<=top;i++){
for(int j=sa[q[i]];j<=sa[q[i]]+ans-;j++) printf("%c",s[j]+'a');
printf("\n");
}
printf("\n");
}
return ;
}
poj 3294 后缀数组 多字符串中不小于 k 个字符串中的最长子串的更多相关文章
- poj 3294 后缀数组+二分
题目大意: 给定n个字符串,求出现在不小于k个字符串中的最长子串 基本思路: 二分长度,统计个数,一般套路,就是这个跟说好的不一样啊,我非得开2倍才不re,真他妈不爽,先二分找出长度,然后根据长度输出 ...
- POJ 3294 后缀数组
题目链接:http://poj.org/problem?id=3294 题意:给定n个字符串,求一个最长子串要求在超过一半的字符串中出现过. 如果多解按字典序输出 思路:根据<<后缀数组— ...
- POJ-3294-Life Forms(后缀数组-不小于 k 个字符串中的最长子串)
题意: 给定 n 个字符串,求出现在不小于 k 个字符串中的最长子串. 分析: 将 n 个字符串连起来,中间用不相同的且没有出现在字符串中的字符隔开,求后缀数组. 然后二分答案,将后缀分成若干组,判断 ...
- poj 3693 后缀数组 重复次数最多的连续重复子串
Maximum repetition substring Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8669 Acc ...
- SPOJ PHRASES 每个字符串至少出现两次且不重叠的最长子串
Description You are the King of Byteland. Your agents have just intercepted a batch of encrypted ene ...
- poj 3415 后缀数组 两个字符串中长度不小于 k 的公共子串的个数
Common Substrings Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 11469 Accepted: 379 ...
- POJ - 3294~Relevant Phrases of Annihilation SPOJ - PHRASES~Substrings POJ - 1226~POJ - 3450 ~ POJ - 3080 (后缀数组求解多个串的公共字串问题)
多个字符串的相关问题 这类问题的一个常用做法是,先将所有的字符串连接起来, 然后求后缀数组 和 height 数组,再利用 height 数组进行求解. 这中间可能需要二分答案. POJ - 3294 ...
- Life Forms POJ - 3294(不小于k个字符串中的最长子串)
题意: 求不小于字符串一半长度个字符串中的最长字串 解析: 论文题例11 将n个字符串连起来,中间用不相同的且没有出现在字符串中的字符隔开, 求后缀数组, 然后二分答案变为判定性问题, 然后判断每组的 ...
- poj 2774 后缀数组 两个字符串的最长公共子串
Long Long Message Time Limit: 4000MS Memory Limit: 131072K Total Submissions: 31904 Accepted: 12 ...
随机推荐
- IDEA2017.3.4破解方式及lombok图文配置详解
下载jetbrainsCrack-2.7-release-str.jar包 下载地址: https://files.cnblogs.com/files/xifenglou/JetBrains.zip ...
- Daily Scrum (2015/11/2)
今日我们完成了博客作业的发布,并且也完成了服务器的配置. 成员 今日工作 时间 明日工作 符美潇 两篇文档的修善和数据库的搭建. 2h 完成数据库搭建,并能爬取数据提供给第二小组使用 潘礼鹏 编写两篇 ...
- java的第二个实验——JAVA面向对象程序设计
java的第二个实验——JAVA面向对象程序设计 北京电子科技学院 实 验 报 告 课程:Java程序设计 班级:1352 姓名:林涵锦 学号:20135213 成绩: ...
- 1001. A+B Format (20)的解题思路以及多源代码文件的尝试编写
前言 这几天刚学了多源代码文件的编译,因为想尝试使用一下这种方法,所以想用此编写这次作业的程序.正好可以learning by doing,在做当中学习新知识.(编译器为Dev-C++) github ...
- Android笔记-4-实现登陆页面并跳转和简单的注册页面
实现登陆页面并跳转和简单的注册页面 首先我们来看看布局的xml代码 login.xml <span style="font-family:Arial;font-size:18px; ...
- NServiceBus官方文档翻译(一)NServiceBus 概况
NServiceBus 概况 NServiceBus 被设计用来组合面向业务的服务,它并不是用来替代诸如 WCF 一类的RPC技术. NServiceBus 不只包含通信模块,像其他成熟的SOA和DD ...
- 在Delphi中获得唯一32位长字符串
function GetGUID: string; var vGUID: TGUID; vTemp:string; begin if S_OK = CreateGuid(vGUID) th ...
- php $_SERVER['HTTP_USER_AGENT']
//获取浏览器 function getBrowse() { global $_SERVER; $Agent = $_SERVER['HTTP_USER_AGENT']; $browseinfo='' ...
- PHP数据库常用常量笔记
参考:http://php.net/manual/zh/pdo.constants.php Warning 自 PHP 5.1 起,开始使用类常量.以前的版本使用类似 PDO_PARAM_BOOL 这 ...
- Best Time to Buy and Sell Stock IV
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...