题目链接在这里洛谷/LOJ

题目大意

有一个串\(S\),每次询问给你一个串\(T\),两个数\(L\)和\(R\),问你\(T\)有多少个本质不同的子串不是\(S[L,R]\)的子串

SOLUTION

如果你做过生成魔咒CF1037H,就会做这道题了

有两个坑点:

1.线段树合并时必须每次都新建结点,因为两颗树都得保留

2.每次失配时必须先尝试减小已经匹配的长度,无法继续减少时再跳\(suflink\)

我的大常数代码

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <random>
#include <string>
#include <vector>
#include <cmath>
#include <ctime>
#include <queue>
#include <map>
#include <set> #define IINF 0x3f3f3f3f3f3f3f3fLL
#define u64 unsigned long long
#define pii pair<int, int>
#define mii map<int, int>
#define u32 unsigned int
#define lbd lower_bound
#define ubd upper_bound
#define INF 0x3f3f3f3f
#define vi vector<int>
#define ll long long
#define mp make_pair
#define pb push_back
#define is insert
#define se second
#define fi first
#define ps push #define $SHOW(x) cout << #x" = " << x << endl
#define $DEBUG() printf("%d %s\n", __LINE__, __FUNCTION__) using namespace std; #define S 500000
#define Q 100000
#define T 1000000
#define mid ((l+r)>>1) int q, n, m, L, R;
char s[S+5], t[T+5];
int nid1 = 1, lst1 = 1, ch1[26][2*S+5], link1[2*S+5], len1[2*S+5], nodecnt, root[2*S+5], ch[2][200*S+5];
int nid, lst, nxt[26][2*T+5], link[2*T+5], len[2*T+5], rest[2*T+5], val[T+5];
ll ans = 0;
int a[2*T+5], buc[2*T+5]; void add(int &o, int l, int r, int x) {
if (!o) o = ++nodecnt;
if (l == r) return ;
if (x <= mid) add(ch[0][o], l, mid, x);
else add(ch[1][o], mid+1, r, x);
} int merge(int x, int y, int l, int r) {
int u = ++nodecnt;
if (x * y == 0) u = x ? x : y;
else {
ch[0][u] = merge(ch[0][x], ch[0][y], l, mid);
ch[1][u] = merge(ch[1][x], ch[1][y], mid + 1, r);
}
return u;
} int query(int u, int l, int r, int L, int R) {
if (!u) return 0;
if (L <= l && r <= R) return 1;
int ret = 0;
if (L <= mid) ret |= query(ch[0][u], l, mid, L, R);
if (R > mid) ret |= query(ch[1][u], mid+1, r, L, R);
return ret;
} void build() {
for (int i = 1, c; i <= n; ++i) {
c = s[i] - 'a';
int cur = ++nid1;
len1[cur] = len1[lst1] + 1;
add(root[cur], 1, n, i);
while (lst1 && !ch1[c][lst1]) ch1[c][lst1] = cur, lst1 = link1[lst1];
if (!lst1) link1[cur] = 1;
else {
int p = lst1, q = ch1[c][lst1];
if (len1[q] == len1[p] + 1) link1[cur] = q;
else {
int clone = ++nid1;
len1[clone] = len1[p] + 1;
for (int j = 0; j < 26; ++j) ch1[j][clone] = ch1[j][q];
link1[clone] = link1[q], link1[cur] = link1[q] = clone;
while (p && ch1[c][p] == q) ch1[c][p] = clone, p = link1[p];
}
}
lst1 = cur;
}
for (int i = 1; i <= nid1; ++i) buc[len1[i]]++;
for (int i = 1; i <= n; ++i) buc[i] += buc[i-1];
for (int i = 1; i <= nid1; ++i) a[buc[len1[i]]--] = i;
for (int i = nid1; i >= 2; --i) root[link1[a[i]]] = merge(root[link1[a[i]]], root[a[i]], 1, n);
} void extend(int c) {
int cur = ++nid;
len[cur] = len[lst] + 1;
for(int i = 0; i < 26; ++i) nxt[i][cur] = 0;
while (lst && !nxt[c][lst]) nxt[c][lst] = cur, lst = link[lst];
if (!lst) link[cur] = 1;
else {
int p = lst, q = nxt[c][lst];
if (len[q] == len[p] + 1) link[cur] = q;
else {
int clone = ++nid;
len[clone] = len[p] + 1;
for (int i = 0; i < 26; ++i) nxt[i][clone] = nxt[i][q];
link[clone] = link[q], link[q] = link[cur] = clone;
while (p && nxt[c][p] == q) nxt[c][p] = clone, p = link[p];
}
}
lst = cur;
} void calc() {
m = strlen(t+1);
lst = nid = 1;
int i, j, c;
for (i = 0; i < 26; ++i) nxt[i][1] = 0;
for (j = 1; j <= m; ++j) extend(t[j] - 'a');
for (i = 2; i <= nid; ++i) rest[i] = 0;
int u = 1, match = 0;
for (i = 1, c; i <= m; ++i) {
c = t[i] - 'a';
while (u && !query(root[ch1[c][u]], 1, n, L + match, R)) {
if(match > len1[link1[u]]) match--;
else u = link1[u], match = len1[u];
}
if (!u) u = 1, match = 0;
else u = ch1[c][u], match++;
val[i] = match;
}
u = 1;
for (i = 1; i <= m; ++i) u = nxt[t[i] - 'a'][u], rest[u] = val[i];
for (i = 0; i <= m; ++i) buc[i] = 0;
for (i = 1; i <= nid; ++i) buc[len[i]]++;
for (i = 1; i <= m; ++i) buc[i] += buc[i-1];
for (i = 1; i <= nid; ++i) a[buc[len[i]]--] = i;
for (i = nid; i >= 2; --i) rest[link[a[i]]] = max(rest[link[a[i]]], rest[a[i]]);
ll ans = 0;
for (i = 2; i <= nid; ++i) ans += max(0, min(len[i] - rest[i], len[i] - len[link[i]]));
printf("%lld\n", ans);
} int main() {
scanf("%s", s+1);
n = strlen(s+1);
build();
scanf("%d", &q);
for (int i = 1; i <= q; ++i) {
scanf("%s%d%d", t+1, &L, &R);
calc();
}
return 0;
}

NOI2018 你的名字——SAM+线段树合并的更多相关文章

  1. [NOI2018]你的名字(SAM+线段树合并)

    考虑l=1,r=n的68分,对S和T建SAM,对T的SAM上的每个节点,计算它能给答案带来多少贡献. T上节点x代表的本质不同的子串数为mx[x]-mx[fa[x]],然后需要去掉所代表子串与S的最长 ...

  2. 【BZOJ5417】[NOI2018]你的名字(线段树,后缀自动机)

    [BZOJ5417][NOI2018]你的名字(线段树,后缀自动机) 题面 BZOJ 洛谷 题解 首先考虑\(l=1,r=|S|\)的做法,对于每次询问的\(T\)串,暴力在\(S\)串的\(SAM\ ...

  3. 【NOI2018】你的名字(SAM & 线段树合并)

    Description Hint Solution 不妨先讨论一下无区间限制的做法. 首先"子串"可以理解为"前缀的后缀",因此我们定义一个 \(\lim(i) ...

  4. UOJ#395. 【NOI2018】你的名字 字符串,SAM,线段树合并

    原文链接https://www.cnblogs.com/zhouzhendong/p/UOJ395.html 题解 记得同步赛的时候这题我爆0了,最暴力的暴力都没调出来. 首先我们看看 68 分怎么做 ...

  5. P4770-[NOI2018]你的名字【SAM,线段树合并】

    正题 题目链接:https://www.luogu.com.cn/problem/P4770 题目大意 给出一个长度为\(n\)的字符串\(S\).\(q\)次询问给出一个串\(T\)和一个区间\([ ...

  6. CF1037H Security——SAM+线段树合并

    又是一道\(SAM\)维护\(endpos\)集合的题,我直接把CF700E的板子粘过来了QwQ 思路 如果我们有\([l,r]\)对应的\(SAM\),只需要在上面贪心就可以了.因为要求的是字典序比 ...

  7. luogu4770 [NOI2018]你的名字 (SAM+主席树)

    对S建SAM,拿着T在上面跑 跑的时候不仅无法转移要跳parent,转移过去不在范围内也要跳parent(注意因为范围和长度有关,跳的时候应该把长度一点一点地缩) 这样就能得到对于T的每个前缀,它最长 ...

  8. 洛谷P4482 [BJWC2018]Border 的四种求法 字符串,SAM,线段树合并,线段树,树链剖分,DSU on Tree

    原文链接https://www.cnblogs.com/zhouzhendong/p/LuoguP4482.html 题意 给定一个字符串 S,有 q 次询问,每次给定两个数 L,R ,求 S[L.. ...

  9. Codeforces 700E. Cool Slogans 字符串,SAM,线段树合并,动态规划

    原文链接https://www.cnblogs.com/zhouzhendong/p/CF700E.html 题解 首先建个SAM. 一个结论:对于parent树上任意一个点x,以及它所代表的子树内任 ...

随机推荐

  1. jstl标签设置通用web项目根路径-备查

    在做项目时(如SSH或SpringMVC),通常需要在很多页面(jsp中的form提交)或者js代码(一般Ajax提交)中用到当前web应用的根路径(拼成访问资源如action/controller. ...

  2. Js 集合运用

    1.给定一个单词good  要求输入 g1 o2 d1 (字母+加字母个数) 方法一: <script type="text/javascript"> var arrO ...

  3. MySQL的安装、配置与优化

    MySQL 安装配置 参考网址:https://www.runoob.com/linux/mysql-install-setup.html MySQL 是最流行的关系型数据库管理系统,由瑞典MySQL ...

  4. Design Circular Deque

    Design your implementation of the circular double-ended queue (deque). Your implementation should su ...

  5. Synchronized&Lock&AQS详解

    加锁目的:由于线程执行的过程是不可控的,所以需要采用同步机制来协同对对象可变状态的访问. 加锁方式:java锁分为两种--显示锁和隐示锁,本质区别在于显示锁需要的是程序员自己手动的进行加锁与解锁如Re ...

  6. C++反汇编第三讲,反汇编中识别继承关系,父类,子类,成员对象

    讲解目录: 1.各类在内存中的表现形式   备注: 主要复习开发知识,和反汇编没有关系,但是是理解反汇编的前提.     2.子类继承父类 2.1 子类中有虚函数,父类中有虚函数 : 都有的情况下   ...

  7. poj 1224

    题意:有一个5 * 6的矩阵,每个位置表示灯,1表示灯亮,0表示灯灭. 然后如果选定位置i,j点击,则位置i,j和其上下左右的灯的状态都会反转. 现在要你求出一个5 * 6的矩阵,1表示这个灯被点击过 ...

  8. oracle wm_concat函数用法

    在Oracle中使用wm_concat(column)可以实现字段的分组合并,逗号分隔.

  9. Java远程通讯可选技术及原理

    转自:https://www.linuxidc.com/index.htm 在分布式服务框架中,一个最基础的问题就是远程服务是怎么通讯的,在Java领域中有很多可实现远程通讯的技术,例如:RMI.MI ...

  10. 简要了解web安全之sql注入

    什么是sql注入? 通俗来讲就是通过 将可执行sql语句作为参数 传入查询sql 中,在sql编译过程中 执行了传入进来的恶意 sql,从而 得到 不应该查到或者不应该执行的sql语句,对网站安全,信 ...