作者:桃李未来 沃若
微信号:
woruo27小红书:信竞教练沃若(
OI_woruo)也可邮件交流:woruo@woruo.online
2025.8.16,如有 Bug 请反馈
AtCoder Beginner Contest 419 题解
A - AtCoder Language
题目大意
给你一个字符串 。
如果 是 red,blue 或 green,分别输出 SSS,FFF 或 MMM。
如果都不是,则输出 Unknown。
题目标签
- 实现
- 选择结构
- 字符串
题解
按题意使用 if 模拟即可。
时间复杂度:。
额外空间复杂度:。
参考代码
#include <bits/stdc++.h>
using namespace std;
string s;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> s;
if (s == "red")
cout << "SSS" << '\n';
else if (s == "blue")
cout << "FFF" << '\n';
else if (s == "green")
cout << "MMM" << '\n';
else
cout << "Unknown" << '\n';
return 0;
}
B - Get Min
题目大意
依次处理 次查询。查询有两种:
- 将给定整数 加入集合。
- 从集合中移除最小的数字并输出。
题目标签
- 实现
- STL
- 堆(优先队列)
题解
使用优先队列(std::priority_queue)模拟即可。
时间复杂度:。
额外空间复杂度:。
参考代码
#include <bits/stdc++.h>
using namespace std;
int n;
priority_queue<int, vector<int>, greater<int>> q;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
for (cin >> n; n--; ) {
int type, x;
cin >> type;
if (type == 1)
cin >> x, q.push(x);
else
cout << q.top() << '\n', q.pop();
}
return 0;
}C - King’s Summit
题目大意
在一张八连通的二维地图上,有 个人分别位于二维地图上的 个整点坐标。
每秒每个人都可以向周围移动一格,求所有人在同一坐标相遇的最短时间。
题目标签
- 数学
- 结论
题解
首先可以发现横纵坐标之间独立,只需考虑一维的情况。
于是考虑数轴上的 个整点有人,所有人相遇的最短时间。这时我们只需要考虑相距最远的两个人,即坐标的最大值和最小值。相遇时间则为极差(最大值减去最小值)除以 上取整。
对横纵坐标分别求解后,答案取较大的一个即可。
时间复杂度:。
额外空间复杂度:。
参考代码
#include <bits/stdc++.h>
using namespace std;
int n;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
int mnr = INT_MAX, mxr, mnc = INT_MAX, mxc;
for (int i = 1, r, c; i <= n; i++) {
cin >> r >> c;
mnr = min(mnr, r), mxr = max(mxr, r);
mnc = min(mnc, c), mxc = max(mxc, c);
}
cout << max(mxr - mnr + 1, mxc - mnc + 1) / 2 << '\n';
return 0;
}D - Substr Swap
题目大意
给定长度为 的小写英文字符串 和 。
随后有 次操作,每次给定区间 ,将 的第 到第 个字符与 的第 到第 个字符一一交换。
输出最终的字符串 。
题目标签
- 前缀和
- 差分
题解
不失一般性地,我们考虑其中第 个字符:如果有偶数个区间覆盖它,则最终它仍然是 ,否则为 。
可以通过维护差分数组再计算前缀和得到每个坐标被覆盖的次数,从而得到答案。
事实上我们只需要判断奇偶性,也可以直接维护前缀异或和,本质上与维护前缀和相同。
时间复杂度:。
额外空间复杂度:。
参考代码
#include <bits/stdc++.h>
using namespace std;
int n, m;
string s;
string t;
int pre[500005];
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
cin >> s;
cin >> t;
for (int i = 1, l, r; i <= m; i++)
cin >> l >> r, pre[l] ^= 1, pre[r + 1] ^= 1;
for (int i = 1; i <= n; i++)
cout << ((pre[i] ^= pre[i - 1]) == 0 ? s[i - 1] : t[i - 1]);
cout << '\n';
return 0;
}E - Subarray Sum Divisibility
题目大意
给定一个长度为 的整数数组。每次操作可以任选其中一个数,将其加一。
求最小操作次数,使得每个长度为 的子段和都能被 整除。
题目标签
- 动态规划
题解
如果有
我们发现,去掉其中求和的公共部分,可以得到 。
这一结论说明,我们可以将所有的位置,按下标对 取模的结果分为 类,每类位置的数对 取模的值必须相同。
如果我们再保证前 个数的和能被 整除,就能让每个长度为 的子段和都能被 整除。
于是我们可以先预处理出将所有下标对 取模结果为 的数,对 取模的值变成 的最小操作次数 。这部分需要 的复杂度。
接着我们设 表示在刚才的前提下,使前 个数的和对 取模为 的最小操作次数。可以写出转移方程:
使用刷表法计算即可。
时间复杂度:。
额外空间复杂度:。
参考代码
#include <bits/stdc++.h>
using namespace std;
int n, m, l;
int a[505];
int val[505][505];
int dp[505][505];
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m >> l;
for (int i = 1; i <= n; i++)
cin >> a[i], a[i] %= m;
for (int i = 1; i <= l; i++)
for (int j = 0; j < m; j++)
for (int k = i; k <= n; k += l)
val[i][j] += (j + m - a[k]) % m;
memset(dp, 0x3f, sizeof(dp));
dp[0][0] = 0;
for (int i = 0; i < l; i++)
for (int j = 0; j < m; j++)
for (int k = 0; k < m; k++)
dp[i + 1][(j + k) % m] = min(dp[i + 1][(j + k) % m], dp[i][j] + val[i + 1][k]);
cout << dp[l][0] << '\n';
return 0;
}F - All Included
题目大意
给定 个小写英文字符串 。
求长度为 的包含所有 为子串的小写英文字符串的方案数,答案为 取模。
题目标签
- 状压DP
- AC自动机
- trie 图
题解
考虑状压DP:当前长度为 ,包含 作为子串的压缩后的状态为 。
我们发现,为了能够在状态直接进行转移,我们还需要维护当前的后缀,才能在追加字符的同时判断输入的字符串 是否出现。
长度为 的后缀最多可能有 种,直接将后缀作为状态并不可行。但实际上有效的后缀很少,我们将这一问题类推到多模式字符串匹配问题,则可以想到利用AC自动机进行优化这一过程。
我们先对所有输入的模式串 建立AC自动机,并加入 trie 图优化。此时状态数只有 种,且在追加字符时可以直接利用 trie 图上的边找到下一个状态。
根据AC自动机的结论,每当经过一个模板串的 endpos 在 fail 树中的子树,这个模板串就出现一次。所以我们还要做一次拓扑排序,以得到 fail 树上每个节点到根路径上,经过的所有模板串状态的并集。
具体实现请阅读参考代码。
时间复杂度:。
额外空间复杂度:。
参考代码
#include <bits/stdc++.h>
using namespace std;
const int MOD = 998'244'353;
struct trie {
int c[26];
int fail;
int d;
int mask;
};
int n, m;
trie t[85];
int tot;
int dp[105][85][1 << 8];
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
for (int i = 0; i < n; i++) {
string s;
cin >> s;
int cur = 0;
for (const auto& j : s) {
int& nxt = t[cur].c[j - 'a'];
if (nxt == 0)
nxt = ++tot;
cur = nxt;
}
t[cur].mask = 1 << i;
}
queue<int> q;
for (const auto& i : t[0].c)
if (i != 0)
q.push(i);
while (!q.empty()) {
auto& cur = t[q.front()];
q.pop();
for (int i = 0; i < 26; i++) {
if (cur.c[i] == 0)
cur.c[i] = t[cur.fail].c[i];
else {
t[cur.c[i]].fail = t[cur.fail].c[i];
t[t[cur.fail].c[i]].d++;
q.push(cur.c[i]);
}
}
}
for (int i = 0; i <= tot; i++)
if (t[i].d == 0)
q.push(i);
vector<int> v;
while (!q.empty()) {
int cur = q.front();
q.pop();
v.push_back(cur);
const int f = t[cur].fail;
if (--t[f].d == 0)
q.push(f);
}
reverse(begin(v), end(v));
for (const auto& i : v)
t[i].mask |= t[t[i].fail].mask;
dp[0][0][0] = 1;
const int K = 1 << n;
for (int i = 0; i < m; i++)
for (int j = 0; j <= tot; j++)
for (int k = 0; k < K; k++)
for (const auto& c : t[j].c)
if ((dp[i + 1][c][k | t[c].mask] += dp[i][j][k]) >= MOD)
dp[i + 1][c][k | t[c].mask] -= MOD;
long long res = 0;
for (int i = 0; i <= tot; i++)
res += dp[m][i][K - 1];
cout << res % MOD << '\n';
return 0;
}