POJ3294Life Forms(广义后缀自动机)(后缀数组+二分+数状数组)
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 ?
题意:
给定一些模板字符串,求一个最长公共字串,这个最长公共字串至少在一半以上的字符串里出现过。
思路:
把模板字符串都连接起来。二分长度,RMQ去验证。
(服了,网上找不到广义后缀自动机的资料。。。)
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<memory>
#include<cmath>
#define maxn 350003
using namespace std;
int n,len,ans,Max,now;
char s[],cap[];
struct SAM
{
int ch[maxn][],fa[maxn],maxlen[maxn],Last,sz;
int root,nxt[maxn],size[maxn];
void init()
{
sz=;
root=++sz;
memset(size,,sizeof(size));
memset(ch[],,sizeof(ch[]));
memset(nxt,,sizeof(nxt));
}
void add(int x)
{
int np=++sz,p=Last;Last=np;
memset(ch[np],,sizeof(ch[np]));
maxlen[np]=maxlen[p]+;
while(p&&!ch[p][x]) ch[p][x]=np,p=fa[p];
if(!p) fa[np]=;
else {
int q=ch[p][x];
if(maxlen[p]+==maxlen[q]) fa[np]=q;
else {
int nq=++sz;
memcpy(ch[nq],ch[q],sizeof(ch[q]));size[nq]=size[q]; nxt[nq]=nxt[q];
maxlen[nq]=maxlen[p]+;
fa[nq]=fa[q];
fa[q]=fa[np]=nq;
while(p&&ch[p][x]==q) ch[p][x]=nq,p=fa[p];
}
}
for(;np;np=fa[np])
if(nxt[np]!=now) {
size[np]++;
nxt[np]=now;
}else break;
}
void dfs(int x,int d){//输出
if(d!=maxlen[x] || d>ans) return;
if(maxlen[x]==ans && size[x]>n){ puts(cap); return; }
for(int i=;i<;++i)
if(ch[x][i]){ cap[d]=i+'a'; dfs(ch[x][i],d+); cap[d]=; }
}
};
SAM Sam;
int main()
{
while(~scanf("%d",&n)&&n){
Sam.init();
for(int i=;i<=n;i++) {
scanf("%s",s+);
Sam.Last=Sam.root;
len=strlen(s+);
now=i;
for(int j=;j<=len;j++) Sam.add(s[j]-'a');
}
Max=;ans=;n>>=;
for(int i=;i<=Sam.sz;i++)
if(Sam.size[i]>n&&Sam.maxlen[i]>ans) { Max=i;ans=Sam.maxlen[i];}
if(ans) Sam.dfs(,);
else puts("?");
puts("");
}
return ;
}
参考:http://blog.csdn.net/JacaJava/article/details/78678612?locationNum=6&fps=1
POJ3294Life Forms(广义后缀自动机)(后缀数组+二分+数状数组)的更多相关文章
- poj 1743 Musical Theme 后缀自动机/后缀数组/后缀树
题目大意 直接用了hzwer的题意 题意:有N(1 <= N <=20000)个音符的序列来表示一首乐曲,每个音符都是1..88范围内的整数,现在要找一个重复的主题."主题&qu ...
- [模板] 后缀自动机&&后缀树
后缀自动机 后缀自动机是一种确定性有限状态自动机, 它可以接收字符串\(s\)的所有后缀. 构造, 性质 翻译自毛子俄罗斯神仙的博客, 讲的很好 后缀自动机详解 - DZYO的博客 - CSDN博客 ...
- 【BZOJ-2527】Meteors 整体二分 + 树状数组
2527: [Poi2011]Meteors Time Limit: 60 Sec Memory Limit: 128 MBSubmit: 831 Solved: 306[Submit][Stat ...
- 【BZOJ3110】【整体二分+树状数组区间修改/线段树】K大数查询
Description 有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c 如果是2 a b c形式,表示询问从第a个位置到第b个位 ...
- BZOJ_3110_[Zjoi2013]K大数查询_整体二分+树状数组
BZOJ_3110_[Zjoi2013]K大数查询_整体二分+树状数组 Description 有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位 ...
- bzoj千题计划316:bzoj3173: [Tjoi2013]最长上升子序列(二分+树状数组)
https://www.lydsy.com/JudgeOnline/problem.php?id=3173 插入的数是以递增的顺序插入的 这说明如果倒过来考虑,那么从最后一个插入的开始删除,不会对以某 ...
- 【bzoj3110】[Zjoi2013]K大数查询 整体二分+树状数组区间修改
题目描述 有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c.如果是2 a b c形式,表示询问从第a个位置到第b个位置,第C大的数 ...
- zoj-3963 Heap Partition(贪心+二分+树状数组)
题目链接: Heap Partition Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge A sequence ...
- 【bzoj4009】[HNOI2015]接水果 DFS序+树上倍增+整体二分+树状数组
题目描述 给出一棵n个点的树,给定m条路径,每条路径有一个权值.q次询问求一个路径包含的所有给定路径中权值第k小的. 输入 第一行三个数 n和P 和Q,表示树的大小和盘子的个数和水果的个数. 接下来n ...
随机推荐
- PHP基础知识学习总结
从今天开始过一遍PHP的基础知识 加油 地址:http://www.runoob.com/php/php-mail.html 该看:PHP发送电子邮件 2017年5月23日23:38:30 ...
- Linux下apache安装php
php 1.下载解压 cd /usr/local/src wget http://mirrors.sohu.com/php/php-5.6.9.tar.gz tar zxvf php-5.6.9.ta ...
- nginx学习之web服务器(四)
1. 定义一个虚拟服务器 http { server { # Server configuration } } 可以在http {}块里面添加多个server {}块,每一个server {}块代表一 ...
- 智能家居DIY-空气质量检测篇-获取温度和湿度篇
目录 智能家居DIY-空气质量检测篇-获取空气污染指数 前言 话说楼主终于升级当爸了,宝宝现在5个月了,宝宝出生的时候是冬天,正是魔都空气污染严重的时候,当时就想搞个自动开启空气净化器,由于种种原因一 ...
- delete 和 truncate 的 区别
如果要清空表中的所有记录,可以使用下面的两种方法: DELETE FROM table1 TRUNCATE TABLE table1 以下 为之区别: 1)执行速度和灵活性 trunca ...
- oracle decode的用法
需求:分别统计emp表中1980,1981,1982,1987年入职的同事的数量. 这里用decode很容易就解决了: select sum(t.num_1980) as "1980&quo ...
- SAP ATP邏輯可用性檢查
[转http://tqmeng.blog.163.com/blog/static/169263916201162002414612/]SAP ATP邏輯可用性檢查1.可用性檢查群組OVZ2主要用於檢查 ...
- 从HttpServletRequest获取POST数据的代码
我们经常需要从HttpServletRequest对象获取POST请求的数据,下面给出简练的代码共大家参考 StringBuffer jb = new StringBuffer(); String l ...
- Linux 3 -grep
七. grep家族: 1. grep退出状态: 0: 表示成功: 1: 表示在所提供的文件无法找到匹配的pattern: 2: 表示参数中提供的文件不存在. 见如下示例: /> grep 'ro ...
- 【ELK】抓取AWS-ELB日志的logstash配置文件
前言 ELK搭建没有难度,难的是logstash的配置文件,logstash主要分为三个部分,input,filter和output. input,输入源可选的输入源由很多,详情见ELK官网,这里我们 ...