当前位置:网站首页>牛客2022 暑期多校3 H Hacker(SAM + 线段树查询区间内部最大子段和)
牛客2022 暑期多校3 H Hacker(SAM + 线段树查询区间内部最大子段和)
2022-08-03 22:50:00 【Morgannr】





题意:
给定 主串 以及 若干副串,副串长度固定,每个位置 都有一个 权值,要求在 主串和副串的公共子串中 找到一个 连续区间,使得 连续区间的权值和最大,求 最大权值和。
思路:
本题其实就是 这两道板子题:SPOJ 2774 Longest Common Substring 和 AcWing 245. 你能回答这些问题吗 硬生生的结合版,
用 SAM 维护 主串 和 枚举的各个副串 的 所有公共子串,对于 主串 与 某个副串 的 某个公共子串所在区间,我们利用 线段树 维护 区间内部最大子段和 即可。
简单题,但是比赛的时候由于没学 SAM 而寄了
不过值得一提的是,代码中的将 主串 和 副串 的 所有公共子串所在区间 提取出来的操作:
int p = 1, t = 0;
for (int j = 1; ss[j]; ++j) {
int sta, ed; //存储每个公共子串所在的合法区间左右端点
int c = ss[j] - 'a';
while (p > 1 && !ch[p][c]) {
//经典匹配操作
p = fa[p];
t = len[p];
}
ed = j - 1; //此时由于当前字符s[j]还未匹配,因此右端点为j-1
if (ch[p][c]) {
//如果当前字符s[j]匹配成功,则右端点移至j,且LCS的长度t++
p = ch[p][c];
++t;
++ed;
}
if (t) {
//当前公共子串长度不为0,即合法,根据其长度t计算左端点sta(因为右端点ed已经求出,相减即可)
sta = ed - t + 1;
}
}
时间复杂度:
O ( n l o g n ) O(nlogn) O(nlogn)
代码:
#include <bits/stdc++.h>
using namespace std;
//#define map unordered_map
#define int long long
int n, m, k;
const int N = 1e5 + 10, M = N << 1;
int fa[M], ch[M][26], len[M], cnt[M];
int np = 1, tot = 1;
char s[N], ss[N];
int w[N];
struct node {
int l, r;
int tmax, sum, lmax, rmax;
} t[N << 2];
void pushup(node& u, node& l, node& r) {
u.sum = l.sum + r.sum;
u.tmax = max(max(l.tmax, r.tmax), l.rmax + r.lmax);
u.lmax = max(l.lmax, l.sum + r.lmax);
u.rmax = max(r.rmax, r.sum + l.rmax);
}
void pushup(int u) {
pushup(t[u], t[u << 1], t[u << 1 | 1]);
}
void build(int u, int l, int r) {
t[u] = {
l, r };
if (l == r) {
t[u].tmax = t[u].sum = t[u].lmax = t[u].rmax = w[l];
return;
}
int mid = l + r >> 1;
build(u << 1, l, mid), build(u << 1 | 1, mid + 1, r);
pushup(u);
}
node ask(int u, int l, int r) {
if (l <= t[u].l && r >= t[u].r) return t[u];
int mid = t[u].l + t[u].r >> 1;
if (r <= mid) return ask(u << 1, l, r);
else if (l >= mid + 1) return ask(u << 1 | 1, l, r);
else {
auto left = ask(u << 1, l, r);
auto right = ask(u << 1 | 1, l, r);
node res;
pushup(res, left, right);
return res;
}
}
void extend(int c) {
int p = np;
np = ++tot;
len[np] = len[p] + 1, cnt[np] = 1;
while (p && !ch[p][c]) {
ch[p][c] = np;
p = fa[p];
}
if (!p) {
fa[np] = 1;
}
else {
int q = ch[p][c];
if (len[q] == len[p] + 1) {
fa[np] = q;
}
else {
int nq = ++tot;
len[nq] = len[p] + 1;
fa[nq] = fa[q], fa[q] = fa[np] = nq;
while (p && ch[p][c] == q) {
ch[p][c] = nq;
p = fa[p];
}
memcpy(ch[nq], ch[q], sizeof ch[q]);
}
}
}
signed main()
{
cin >> n >> m >> k;
cin >> s + 1;
for (int i = 1; i <= n; ++i) {
extend(s[i] - 'a');
}
for (int i = 1; i <= m; ++i) {
scanf("%lld", &w[i]);
}
build(1, 1, m);
for (int i = 0; i < k; ++i) {
scanf("%s", ss + 1);
int p = 1, t = 0;
int res = -2e18;
for (int j = 1; ss[j]; ++j) {
int sta, ed;
int c = ss[j] - 'a';
while (p > 1 && !ch[p][c]) {
p = fa[p];
t = len[p];
}
ed = j - 1;
if (ch[p][c]) {
p = ch[p][c];
++t;
++ed;
}
if (t) {
sta = ed - t + 1;
res = max(max(res, ask(1, sta, ed).tmax), (int)0);
}
}
printf("%lld\n", res);
}
return 0;
}
边栏推荐
- Golang Chapter 1: Getting Started
- pikachu Over permission
- With the rise of concepts such as metaverse and web3.0, many digital forms such as digital people and digital scenes have begun to appear.
- UVa 10003 - Cutting Sticks(白书,区间DP)
- Another MySQL masterpiece published by Glacier (send the book at the end of the article)!!
- 代码随想录笔记_动态规划_416分割等和子集
- UVa 1025 - A Spy in the Metro(白书)
- Teach a Man How to Fish - How to Query the Properties of Any SAP UI5 Control by Yourself Documentation and Technical Implementation Details Demo
- Binary search tree to solve the fallen leaves problem
- 九种方式,教你读取 resources 目录下的文件路径
猜你喜欢

关于IDO预售系统开发技术讲解丨浅谈IDO预售合约系统开发原理分析

2022-08-02 mysql/stonedb slow SQL-Q18 - memory usage surge analysis

ML之interpret:基于titanic泰坦尼克是否获救二分类预测数据集利用interpret实现EBC模型可解释性之全局解释/局部解释案例

The principle and use of AOSP CameraLatencyHistogram

V8中的快慢数组(附源码、图文更易理解)

静态文件快速建站

Click the icon in Canvas App to generate PDF and save it to Dataverse

MiniAPI of .NET6 (14): Cross-domain CORS (Part 1)

LabVIEW代码生成错误 61056

Cisco ike2 IPSec configuration
随机推荐
Teach a Man How to Fish - How to Query the Properties of Any SAP UI5 Control by Yourself Documentation and Technical Implementation Details Demo
113. 授人以渔 - 如何自行查询任意 SAP UI5 控件属性的文档和技术实现细节
V8中的快慢数组(附源码、图文更易理解)
override学习(父类和子类)
冰河又一MySQL力作出版(文末送书)!!
Bytebase database schema change management tool
Republish the lab report
代码随想录笔记_动态规划_416分割等和子集
Why do we need callbacks
Network basic learning series four (network layer, data link layer and some other important protocols or technologies)
The principle and use of AOSP CameraLatencyHistogram
With 4 years of work experience, the 5 communication methods between multi-threads can't be said, can you believe it?
for loop exercises
Golang第二章:程序结构
Testng listener
易观分析:2022年Q2中国网络零售B2C市场交易规模达23444.7亿元
授人以渔 - 如何自行查询任意 SAP UI5 控件属性的文档和技术实现细节试读版
Basic Concepts of Graphs
websocket多线程发送消息报错TEXT_PARTIAL_WRITING--自旋锁替换synchronized独占锁的使用案例
重发布实验报告