基因

给定一个长度为 \(n\) 的字符串 \(s\),有 \(q\) 组询问,每个询问给定 \(l,r\),询问 \(s[l..r]\) 中有多少本质不同的回文子串。

强制在线。\(n\leq 10^5,q ≤ 2n\)。

题解

翁文涛《回文树及其应用》。

“维护每个关键点到每个右端点的回文树”指的是维护 \((i,i\sim n)\) 的回文树。

往前端添加字符,\(fail\) 和 \(quick\) 都同后端一样。

回文树比较好的性质是加入一个之后节点数最多变化1,所以不用每次清空来处理答案。初始化的时候维护出了quick的话,以后就不用维护了,所以分块的复杂度少了一个 O(Σ)。

往前端加入字符的时候需要判断新增的节点是不是之前就出现过了的,这个可以对每个块维护每个节点第一次时间(未出现为 inf)。换种说法:为了去重,还需要记录一下Pam上每个节点在每个关键点为左端点插入到时候到最左边出现位置。

#include <bits/stdc++.h>
using namespace std; const int N = 101010;
const int M = 350; inline char get(void) {
static char buf[100000], *S = buf, *T = buf;
if (S == T) {
T = (S = buf) + fread(buf, 1, 100000, stdin);
if (S == T) return EOF;
}
return *S++;
}
template<typename T>
inline void read(T &x) {
static char c; x = 0; int sgn = 0;
for (c = get(); c < '0' || c > '9'; c = get()) if (c == '-') sgn = 1;
for (; c >= '0' && c <= '9'; c = get()) x = x * 10 + c - '0';
if (sgn) x = -x;
}
inline void read(char *s) {
static char c; int len = 0;
for (c = get(); c < 'a' || c > 'z'; c = get());
for (; c >= 'a' && c <= 'z'; c = get()) s[len++] = c;
s[len] = 0;
} char s[N];
int last_back, last_front, total;
int fail[N], len[N], ri[N];
int ch[N][30], quick[N][30];
int block_front[M][N], ans[M][N], block_time[M][N];
int visit[N];
int type, lastans, clocks;
int n, q, l, r, B;
int belong[N]; inline void ExtendFront(int pos, int r) {
int p = last_front, key = s[pos];
if (s[pos + len[p] + 1] != key || pos + len[p] + 1 > r) p = quick[p][key];
if (!ch[p][key]) {
int np = ++total, q = fail[p];
len[np] = len[p] + 2;
if (s[pos + len[q] + 1] == key) fail[np] = ch[q][key];
else fail[np] = ch[quick[q][key]][key];
memcpy(quick[np], quick[fail[np]], sizeof quick[np]);
quick[np][s[pos + len[fail[np]]]] = fail[np];
ch[p][key] = np;
}
last_front = ch[p][key];
if (len[last_front] == r - pos + 1) last_back = last_front;
}
inline void ExtendBack(int l, int pos) {
int p = last_back, key = s[pos];
if (s[pos - len[p] - 1] != key || pos - len[p] - 1 < l) p = quick[p][key];
if (!ch[p][key]) {
int np = ++total, q = fail[p];
len[np] = len[p] + 2;
if (s[pos - len[q] - 1] == key) fail[np] = ch[q][key];
else fail[np] = ch[quick[q][key]][key];
memcpy(quick[np], quick[fail[np]], sizeof quick[np]);
quick[np][s[pos - len[fail[np]]]] = fail[np];
ch[p][key] = np;
}
last_back = ch[p][key];
if (len[last_back] == pos - l + 1) last_front = last_back;
} int main(void) {
read(type); read(n); read(q);
read(s + 1); s[0] = -1;
for (int i = 1; i <= n; i++) s[i] -= 'a';
B = sqrt(n);
for (int i = 1; i <= n; i++) belong[i] = (i - 1) / B + 1; memset(block_time, 0x3f, sizeof block_time);
fail[0] = fail[1] = 1; len[1] = -1; total = 1;
for (int i = 0; i < 26; i++) quick[0][i] = quick[1][i] = 1; for (int i = 0; i < belong[n]; i++) {
++clocks; last_back = last_front = 0;
int st = i * B + 1;
for (int j = st; j <= n; j++) {
ExtendBack(st, j);
block_front[i][j] = last_front; ans[i][j] = ans[i][j - 1];
if (visit[last_back] != clocks) {
++ans[i][j]; visit[last_back] = clocks;
block_time[i][last_back] = j;
}
}
} while (q--) {
read(l); read(r); ++clocks;
l ^= type * lastans;
r ^= type * lastans;
if (l > r) swap(l, r);
if (belong[l] == belong[r]) {
last_back = last_front = 0; lastans = 0;
for (int i = r; i >= l; i--) {
ExtendFront(i, r);
if (visit[last_front] != clocks) {
++lastans; visit[last_front] = clocks;
}
}
} else {
lastans = ans[belong[l]][r];
last_front = block_front[belong[l]][r];
for (int i = belong[l] * B; i >= l; i--) {
ExtendFront(i, r);
if (visit[last_front] != clocks) {
visit[last_front] = clocks;
if (block_time[belong[l]][last_front] > r) ++lastans;
}
}
}
printf("%d\n", lastans);
}
return 0;
}

LOJ6070 基因的更多相关文章

  1. LOJ6070 基因 分块+回文自动机

    这个在翁文涛的论文里有讲到 大概的就是一个子串的回文自动机是原串回文自动机的子图 于是每隔$\sqrt n$重新跑一个$(k \times \sqrt n,n)$的回文自动机 记录回文串个数和位置 并 ...

  2. 使用limma、Glimma和edgeR,RNA-seq数据分析易如反掌

    使用limma.Glimma和edgeR,RNA-seq数据分析易如反掌 Charity Law1, Monther Alhamdoosh2, Shian Su3, Xueyi Dong3, Luyi ...

  3. loj6070【山东集训第一轮Day4】基因

    题解: 分块对每个块的起点$st[i]$到$n$做一次回文自动机; 由于子串的回文自动机是原串的子图,所以并不需要重新构图,在原来的图上做即可: 做的时候记录某个终点的本质不同的回文串和$sum[i] ...

  4. 基因匹配(bzoj 1264)

    Description 基因匹配(match) 卡卡昨天晚上做梦梦见他和可可来到了另外一个星球,这个星球上生物的DNA序列由无数种碱基排列而成(地球上只有4种),而更奇怪的是,组成DNA序列的每一种碱 ...

  5. 1264: [AHOI2006]基因匹配Match

    1264: [AHOI2006]基因匹配Match Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 982  Solved: 635[Submit][S ...

  6. 【GWAS文献】基于GWAS与群体进化分析挖掘大豆相关基因

    Resequencing 302 wild and cultivated accessions identifies genes related to domestication and improv ...

  7. AC日记——基因相关性 openjudge 1.7 03

    03:基因相关性 总时间限制:  1000ms 内存限制:   65536kB 描述 为了获知基因序列在功能和结构上的相似性,经常需要将几条不同序列的DNA进行比对,以判断该比对的DNA是否具有相关性 ...

  8. luogu[1140]相似基因

    题目背景 大家都知道,基因可以看作一个碱基对序列.它包含了4种核苷酸,简记作A,C,G,T.生物学家正致力于寻找人类基因的功能,以利用于诊断疾病和发明药物. 在一个人类基因工作组的任务中,生物学家研究 ...

  9. 用excel绘制基因芯片热力图

    1.  首先我们通过一些方法得到了如下的数据,基于篇幅以及为了教学隐去了其他一些信息. 2. 选中表达数据,执行 开始—条件格式—色阶 选择一个合适的色阶: 3. 选择好颜色之后得到了如下结果:

随机推荐

  1. 鼠标滚轮滚动慢/拖动Office出现滞后问题处理

    一.说明 我对外设一直不是很了解,买的鼠标今天到了,使用时遇到了两个问题在这里记一下. 二.滚轮滚动慢处理 问题描述:在网页中滚动滚轮每次只能上下移动一点点,感觉很难受. 问题原因:此问题是滚轮滚动一 ...

  2. [转帖]华为PC端台式机电脑来啦!自研主板及自研CPU处理器

    华为PC端台式机电脑来啦!自研主板及自研CPU处理器 在性能上,4核版相当于酷睿i5 ,8核版相当于酷睿i5 8300H. https://www.bilibili.com/read/cv376376 ...

  3. Springboot Actuator之五:Springboot中的HealthAggregator、新增自定义Status

    springboot的actuator内置了/health的endpoint,很方便地规范了每个服务的健康状况的api,而且HealthIndicator可以自己去扩展,增加相关依赖服务的健康状态,非 ...

  4. golang 数据导出excel (github.com/360EntSecGroup-Skylar/excelize)

    package handler import ( "fmt" "git.shannonai.com/public_info_prophet/prophet_risk_ag ...

  5. python selenium IE Firxfor pyinstaller

    以前在python环境下selenium 主要用的是chromdriver,这次发现老是报错(Timeout), 实际又是正确的, 可能是和chrome版本不正确,再加上我程序蹦来就在windows环 ...

  6. lucene字典实现原理(转)

    原文:https://www.cnblogs.com/LBSer/p/4119841.html 1 lucene字典 使用lucene进行查询不可避免都会使用到其提供的字典功能,即根据给定的term找 ...

  7. CentOS 7.0 更改SSH 远程连接 端口号

    许多学习过redhat 7的同学们,在使用centos的时候总会遇到一些问题,因为centos在安装时会默认开启一些服务,今天我们就来更改下centos 7.0的SSH端口. 操作步骤: 远程登录到c ...

  8. 8、VUE自定义组件

    1.为什么要使用自定义组件? 自定义组件是用来封装复杂的内容,提高可重用性,比如封装复杂的表格组件.日历组件.图片轮播组件等. 2.自定义组件 2.1. 全局组件 全局组件是每个Vue对象都能使用的组 ...

  9. Django---ORM的常用字段和自定义字段,DjangoORM字段与数据库类型对应,字段参数和Meta的参数,Django的admin操作,13中orm操作方法,单标的双下方法

    Django---ORM的常用字段和自定义字段,DjangoORM字段与数据库类型对应,字段参数和Meta的参数,Django的admin操作,13中orm操作方法,单标的双下方法 一丶ORM常用字段 ...

  10. Promise介绍及使用场景

    Promise 介绍 Promise 是一个构造函数,是异步编程的一种解决方案.所谓Promse,它本身就是一个容器,里面保存着异步操作的结果,对的,这和回调函数类似. Promise 容器本身不是异 ...