嘟嘟嘟




省选Day1真是重大失误,T2连暴力都没时间写。

上周五重新答了遍Day1,竟然搞了187分吼吼吼吼。

T2按40分写的暴力,结果竟然得了60分。




稍微说一下暴力吧:预处理哈希,对于一组支配关系\(A_i\), \(B_i\),用哈希判断\(B_i\)是哪些\(A\)串的前缀,是的话就连边\((A_i, A_j)\)。用哈希能做到单次\(O(n)\),因此建图复杂度\(O(n ^ 2)\)。

然后就是判无环后拓扑排序了。

暴力代码我会在最下面放!




接着讲正解。

从暴力能看出来,瓶颈在于建图,一是挨个判前缀时间不够,二是挨个建边空间不够。

为了解决这些,我们需要强力的字符串数据结构,比如SAM(我不会SA)。




首先把原串反过来建SAM,并记录每一个位置在后缀树(我才知道到由link构成的树就是后缀树)上对应的节点。

然后对于每一个\(A\),\(B\)串,倍增找到后缀树上的所属的节点,开一个vector存下来。

我们要做的,就是对于所有\(B_i\),如果是\(A_j\)的后缀(因为反过来了),就向\(A_j\)连边,这样如果\(A_i\)支配\(B_i\),只需从\(A_i\)向\(B_i\)连边,图就建好了。

支配的话直接连就好了,而\(B_i\)向所有\(A_j\)连边,其实就是\(B_i\)向他的子树里的所有\(A_j\)连边,但这样一条条连固然不行,观察到这些\(A_j\)的dfs序是连续的,所以可以线段树优化建图,这就是伟大的学姐写的。

其实也不用。只要每一个点向他的孩子连边就行了,这样虽然不是直接连边,但肯定能保证\(B_i\)能到\(A_j\)。

这个虽然解决了,但还有一件事没有解决。

就是后缀树上的每一个点可能有多个\(A\),\(B\)串(不然开vector干嘛),因此上述连边会让编号弄混,分不清到底向哪一个串连边。

所以我们先把每一个vector按长度为第一关键字,是否为\(A\)类串为第二关键字排序,然后把后缀树上的点拆点,在一个节点内,如果有\(A_1, A_2, A_3, B_1, B_2\)这几个串,且长度上满足\(l_{B_1} > l_{A_1} > l_{A_2} > l_{B_2} > l _ {A_3}\),那么我们就这么建图:



其中\(i\)表示后缀树上这个结点的编号,\(j\)是\(i\)的孩子结点。

然后我们就可以愉快的跑拓扑排序啦,注意到图上只有\(A\)类点是我们需要的,因此把别的点的权值清零,就不会干扰\(A\)类点统计答案了。

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define In inline
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 2e5 + 5;
const int maxe = 2e6 + 5;
const int N = 18;
inline ll read()
{
ll ans = 0;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) last = ch, ch = getchar();
while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < 0) x = -x, putchar('-');
if(x >= 10) write(x / 10);
putchar(x % 10 + '0');
} char s[maxn];
int n, m, na, nb; int fa[N + 2][maxn << 1];
int tra[maxn << 1][27], link[maxn << 1], len[maxn << 2], id[maxn], las = 1, cnt = 1;
In void insert(int c)
{
int now = ++cnt, p = las;
len[now] = len[las] + 1;
while(p && !tra[p][c]) tra[p][c] = now, p = link[p];
if(!p) link[now] = 1;
else
{
int q = tra[p][c];
if(len[q] == len[p] + 1) link[now] = q;
else
{
int clo = ++cnt;
memcpy(tra[clo], tra[q], sizeof(tra[q]));
len[clo] = len[p] + 1;
link[clo] = link[q]; link[q] = link[now] = clo;
while(p && tra[p][c] == q) tra[p][c] = clo, p = link[p];
}
}
las = now;
} int Siz = 0, isa[maxn << 2], lst[maxn << 2];
int a[maxn], b[maxn];
vector<int> v[maxn << 2];
In void solve(const int& flg)
{
int L = read(), R = read();
int Len = R - L + 1, x = id[L];
for(int i = N; i >= 0; --i)
if(fa[i][x] && len[fa[i][x]] >= Len) x = fa[i][x];
isa[++Siz] = flg, len[Siz] = Len, v[x].push_back(Siz);
} In bool cmp(const int& a, const int& b)
{
return len[a] > len[b] || (len[a] == len[b] && isa[a] > isa[b]);
} struct Edge
{
int nxt, to;
}e[maxe];
int head[maxn << 2], ecnt = -1, du[maxn << 2];
In void addEdge(const int& x, const int& y)
{
++du[y];
e[++ecnt] = (Edge){head[x], y};
head[x] = ecnt;
} ll dp[maxn << 2];
In ll topo()
{
queue<int> q; ll ret = 0;
for(int i = 1; i <= Siz; ++i) if(!du[i]) q.push(i), dp[i] = len[i];
while(!q.empty())
{
int now = q.front(); q.pop();
ret = max(ret, dp[now]);
for(int i = head[now], v; ~i; i = e[i].nxt)
{
v = e[i].to;
dp[v] = max(dp[v], dp[now] + len[v]);
if(!--du[v]) q.push(v);
}
}
for(int i = 1; i <= Siz; ++i) if(du[i]) return -1;
return ret;
} In void clear()
{
for(int i = 1; i <= cnt; ++i) link[i] = 0, Mem(tra[i], 0);
ecnt = -1; las = cnt = 1;
for(int i = 1; i <= Siz; ++i)
{
v[i].clear(); head[i] = -1;
isa[i] = len[i] = dp[i] = du[i] = 0;
}
} int main()
{
//freopen("ha.in", "r", stdin);
//freopen("ha.out", "w", stdout);
Mem(head, -1);
int T = read();
while(T--)
{
scanf("%s", s + 1); n = strlen(s + 1);
for(int i = n; i; --i) insert(s[i] - 'a'), id[i] = las;
for(int i = 1; i <= cnt; ++i) fa[0][i] = link[i];
for(int j = 1; j <= N; ++j)
for(int i = 1; i <= cnt; ++i) fa[j][i] = fa[j - 1][fa[j - 1][i]];
Siz = cnt;
na = read();
for(int i = 1; i <= na; ++i) solve(1), a[i] = Siz;
nb = read();
for(int i = 1; i <= nb; ++i) solve(0), b[i] = Siz;
for(int i = 1; i <= cnt; ++i) sort(v[i].begin(), v[i].end(), cmp);
for(int i = 1; i <= cnt; ++i)
{
int last = i;
for(int j = v[i].size() - 1; j >= 0; --j)
{
addEdge(last, v[i][j]);
if(!isa[v[i][j]]) last = v[i][j];
}
lst[i] = last;
}
for(int i = 2; i <= cnt; ++i) addEdge(lst[link[i]], i);
for(int i = 1; i <= Siz; ++i) if(!isa[i]) len[i] = 0;
m = read();
for(int i = 1, x, y; i <= m; ++i) x = read(), y = read(), addEdge(a[x], b[y]);
write(topo()), enter;
clear();
}
return 0;
}



然后还有我的优美的暴力代码啦啦啦。(判环的时候还特意写了个tarjan,其实不用,拓扑后看有没有入度不为0的点就行了,就像上面一样)
```c++
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define In inline
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const ull bse = 998244353;
const int maxn = 2e5 + 5;
const int maxe = 1e7 + 5;
inline ll read()
{
ll ans = 0;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) last = ch, ch = getchar();
while(isdigit(ch)) ans = (ans = 10) write(x / 10);
putchar(x % 10 + '0');
}
In void MYFILE()
{
#ifndef mrclr
freopen("string.in", "r", stdin);
freopen("string.out", "w", stdout);
#endif
}

char s[maxn];

ull has[maxn], p[maxn];

int n, m, na, nb, du[maxn];

bool FLG = 1;

struct Node

{

int L, R, len; ull h;

}ta[maxn], tb[maxn];

In ull calc_has(int L, int len)

{

return has[L + len - 1] - has[L - 1] * p[len];

}

struct Edge

{

int nxt, to;

}e[maxe];

int head[maxn], ecnt = -1;

In void addEdge(int x, int y)

{

if(x == y) FLG = 0;

e[++ecnt] = (Edge){head[x], y};

head[x] = ecnt;

}

bool in[maxn];

int st[maxn], top = 0;

int dfn[maxn], low[maxn], cnt = 0;

int num[maxn], ccol = 0;

In void tarjan(int now)

{

dfn[now] = low[now] = ++cnt;

st[++top] = now; in[now] = 1;

for(int i = head[now], v; ~i; i = e[i].nxt)

{

if(!dfn[v = e[i].to])

{

tarjan(v);

low[now] = min(low[now], low[v]);

}

else if(in[v]) low[now] = min(low[now], dfn[v]);

}

if(low[now] == dfn[now])

{

int x; ++ccol;

do

{

x = st[top--]; in[x] = 0;

++num[ccol];

}while(x ^ now);

}

}

ll dis[maxn];

In void topo()

{

queue q; q.push(0);

for(int i = 1; i <= na; ++i) if(!du[i]) q.push(i), dis[i] = ta[i].len;

while(!q.empty())

{

int now = q.front(); q.pop();

for(int i = head[now], v; ~i; i = e[i].nxt)

{

v = e[i].to;

dis[v] = max(dis[v], dis[now] + ta[v].len);

if(!--du[v]) q.push(v);

}

}

}

In void init()

{

ecnt = -1, top = cnt = ccol = 0; FLG = 1;

int Max = max(na, nb);

for(int i = 0; i <= Max; ++i)

du[i] = dfn[i] = low[i] = num[i] = dis[i] = in[i] = 0, head[i] = -1;

}

int main()

{

//MYFILE();

Mem(head, -1);

int T = read();

while(T--)

{

scanf("%s", s + 1);

n = strlen(s + 1); p[0] = 1;

for(int i = 1; i <= n; ++i)

{

has[i] = has[i - 1] * bse + s[i];

p[i] = p[i - 1] * bse;

}

na = read();

for(int i = 1; i <= na; ++i)

{

int L = read(), R = read();

ta[i] = (Node){L, R, R - L + 1, has[R] - has[L - 1] * p[R - L + 1]};

}

nb = read();

for(int i = 1; i <= nb; ++i)

{

int L = read(), R = read();

tb[i] = (Node){L, R, R - L + 1, has[R] - has[L - 1] * p[R - L + 1]};

}

init();

m = read();

for(int i = 1; i <= m; ++i)

{

int x = read(), y = read();

for(int j = 1; j <= na && FLG; ++j)

if(ta[j].len >= tb[y].len && calc_has(ta[j].L, tb[y].len) == tb[y].h)

addEdge(x, j), ++du[j];

}

if(!FLG) {puts("-1"); continue;}

bool flg = 1;

for(int i = 1; i <= na; ++i) if(!dfn[i]) tarjan(i);

for(int i = 1; i <= ccol && flg; ++i) if(num[i] > 1) flg = 0;

if(!flg) {puts("-1"); continue;}

topo();

ll ans = 0;

for(int i = 1; i <= na; ++i) ans = max(ans, dis[i]);

write(ans), enter;

}

return 0;

}

[十二省联考2019]D1T2字符串问题的更多相关文章

  1. LOJ #3049. 「十二省联考 2019」字符串问题

    LOJ #3049. 「十二省联考 2019」字符串问题 https://loj.ac/problem/3049 题意:给你\(na\)个\(A\)类串,\(nb\)个\(B\)类串,\(m\)组支配 ...

  2. 「十二省联考 2019」字符串问题——SAM+DAG

    题目 [题目描述] Yazid 和 Tiffany 喜欢字符串问题.在这里,我们将给你介绍一些关于字符串的基本概念. 对于一个字符串 $S$, 我们定义 $\lvert S\rvert$ 表示 $S$ ...

  3. LOJ 3049: 洛谷 P5284: 「十二省联考 2019」字符串问题

    题目传送门:LOJ #3049. 题意简述: 给定一个长度为 \(n\) 的母串 \(S\). 有 \(n_a\) 个 A 类串,都是 \(S\) 的子串,以区间的形式给出. 有 \(n_b\) 个 ...

  4. 【LOJ 3049】「十二省联考 2019」字符串问题

    这个D1T2绝对有毒... 首先我们构造一把反串的后缀自动机. 然后我们就需要找到每一个子串在SAM上的节点. 这个可以通过扫描线+树上倍增处理. 首先我们把所有的子串按照左端点排序, 然后从右往左扫 ...

  5. 「ZJOI2019」&「十二省联考 2019」题解索引

    「ZJOI2019」&「十二省联考 2019」题解索引 「ZJOI2019」 「ZJOI2019」线段树 「ZJOI2019」Minimax 搜索 「十二省联考 2019」 「十二省联考 20 ...

  6. [十二省联考2019]字符串问题——后缀自动机+parent树优化建图+拓扑序DP+倍增

    题目链接: [十二省联考2019]字符串问题 首先考虑最暴力的做法就是对于每个$B$串存一下它是哪些$A$串的前缀,然后按每组支配关系连边,做一遍拓扑序DP即可. 但即使忽略判断前缀的时间,光是连边的 ...

  7. 【BZOJ5496】[十二省联考2019]字符串问题(后缀树)

    [BZOJ5496][十二省联考2019]字符串问题(后缀树) 题面 BZOJ 洛谷 题解 首先显然可以把具有支配关系的串从\(A\)到\(B\)连一条有向边,如果\(B_i\)是\(A_j\)的前缀 ...

  8. [十二省联考2019]异或粽子——可持久化trie树+堆

    题目链接: [十二省联考2019]异或粽子 求前$k$大异或区间,可以发现$k$比较小,我们考虑找出每个区间. 为了快速得到一个区间的异或和,将原序列做前缀异或和. 对于每个点作为右端点时,我们维护出 ...

  9. 【BZOJ5495】[十二省联考2019]异或粽子(主席树,贪心)

    [BZOJ5495][十二省联考2019]异或粽子(主席树,贪心) 题面 BZOJ 洛谷 题解 这不是送分题吗... 转异或前缀和,构建可持久化\(Trie\). 然后拿一个堆维护每次的最大值,每次如 ...

随机推荐

  1. kubernetes 安装备注

    一.安装环境 阿里云:centos 7.3 master节点:外网IP(116.62.205.90).内网IP(172.16.223.200) node节点:外网IP(116.62.212.174). ...

  2. Mac 下 Chrome 浏览器 ERR_NETWORK_CHANGED 报错解决方案

    一直以为是 SwitchyOmega 和 SpechtLite 的问题,原来是支付宝安全控件. 由于支付宝现在已经不需要 Mac 安全控件机制,所以可以通过在 terminal 运行以下命令来移除 s ...

  3. 在asp.net core2.1中添加中间件以扩展Swashbuckle.AspNetCore3.0支持简单的文档访问权限控制

    Swashbuckle.AspNetCore3.0 介绍 一个使用 ASP.NET Core 构建的 API 的 Swagger 工具.直接从您的路由,控制器和模型生成漂亮的 API 文档,包括用于探 ...

  4. 聊聊Mysql索引和redis跳表

    摘要 面试时,交流有关mysql索引问题时,发现有些人能够涛涛不绝的说出B+树和B树,平衡二叉树的区别,却说不出B+树和hash索引的区别.这种一看就知道是死记硬背,没有理解索引的本质.本文旨在剖析这 ...

  5. string类的几种方法

    string str="123abc456";int i=3;1 取字符串的前i个字符   str=str.Substring(0,i); // or  str=str.Remov ...

  6. 机器学习——决策树,DecisionTreeClassifier参数详解,决策树可视化查看树结构

    0.决策树 决策树是一种树型结构,其中每个内部节结点表示在一个属性上的测试,每一个分支代表一个测试输出,每个叶结点代表一种类别. 决策树学习是以实例为基础的归纳学习 决策树学习采用的是自顶向下的递归方 ...

  7. VC6.0打开或添加工程时崩溃的解决方法

    官方解决办法(英文):http://support.microsoft.com/kb/241396/en-us 网友解决(中文):http://blog.163.com/wjatnx@yeah/blo ...

  8. 一个简洁的小H车调运模型

    一个简洁的小H车调运模型 不久前, 帝都B城市到处都是小H车, 理想的小H车应该是布朗运动\均匀分布,可是现实上它们就是不均匀.于是有如下问题: 观察帝都 HD区SY村区域,将其划分成10个用车点,用 ...

  9. 补习系列(1)-springboot项目基础搭建课

    目录 前言 一.基础结构 二.添加代码 三.应用配置 四.日志配置 五.打包部署 小结 前言 springboot 最近火的不行,目前几乎已经是 spring 家族最耀眼的项目了.抛开微服务.技术社区 ...

  10. [JavaScript] 函数节流(throttle)和函数防抖(debounce)

    js 的函数节流(throttle)和函数防抖(debounce)概述 函数防抖(debounce) 一个事件频繁触发,但是我们不想让他触发的这么频繁,于是我们就设置一个定时器让这个事件在 xxx 秒 ...