题解:P11482 [NordicOI 2021] Pearls

· · 题解

题目传送门

思路

::::error[16\text{pts}]{open} 首先想到的就是暴力。暴力匹配 A_i 和 B_j 存入答案,然后查询,时间复杂度 O(la \times lb + q),而题目中 la,lb 最大可以达到 2 \times 10^5,不仅会超时,而且也无法存下 2 \times \left(2 \times {10^5}\right)^2=8 \times 10^{10} 个字符,预估得分 16\text{pts}。 :::: ::::success[100\text{pts}]{open} 考虑二分 + 前缀和。

我们可以使用 set<pair<char, char>> 来预处理每个「丑陋颜色对」,由于 A 仅由小写字母组成,我们可以用 i 枚举 \texttt{a} 到 \texttt{z} 的 26 个字母,然后用 j 枚举 B 的每一个字母,如果非「丑陋颜色对」,就将 i 与 B_j 加入答案数组内。

然后进行前缀和,用 pre_i 表示前 i 个 A 中的字符贡献的总长度,得:

pre_i=\begin{cases} 0, \hspace{3.5cm} i=0\\ pre_{i-1}+|ans_{A_{i-1}}|,\hspace{1.07cm}i \geq 1 \end{cases}
最后使用 upper_bound 进行二分,输出答案即可,预估得分 100\text{pts}。
#### 代码
::::success[AC Code]
```cpp line-numbers
// Problem: P11482 [NordicOI 2021] Pearls
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P11482
// Memory Limit: 512 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)

// Code by wangxy0311

include <bits/stdc++.h>

define int long long // 不开long long见祖宗

using namespace std;

mt19937 rnd(std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now().time_since_epoch()).count()); template <class T> T randint(T l, T r) { uniform_int_distribution<T> dist(l, r); return dist(rnd); }

typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef int128 i128; typedef unsigned int128 u128; typedef long double ld;

define rep(x, y, z) for (int x = (y); x <= (z); ++x)

define per(x, y, z) for (int x = (y); x >= (z); --x)

define debug(...) fprintf(stderr, __VA_ARGS__)

define fileIO(s) \

freopen(s ".in", "r", stdin); \ freopen(s ".out", "w", stdout);

define endl '\n'

define all(x) begin(x), end(x)

inline void fast_io() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); } ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

template <typename T> void chkmax(T &a, T b) { if (b > a) a = b; } template <typename T> void chkmin(T &a, T b) { if (b < a) a = b; }

template <int MOD> struct Modint { ll val; Modint(ll v = 0) { val = (v % MOD + MOD) % MOD; } Modint operator+(const Modint & x) const { return Modint(val + x.val); } Modint operator-(const Modint & x) const { return Modint(val - x.val); } Modint operator(const Modint & x) const { return Modint((i128)val x.val % MOD); } Modint operator^(ll b) const { Modint res = 1, a = this; for (; b; b >>= 1, a = a) if (b & 1) res = a; return res; } Modint operator^(const Modint & b) const { return this ^ b.val; } Modint operator~() const { return this ^ MOD - 2; } Modint operator/(const Modint & x) const { return this ~x; } Modint operator-() const { return Modint(-val); } Modint & operator+=(const Modint & x) { val = (val + x.val) % MOD; return this; } Modint & operator-=(const Modint & x) { val = (val - x.val + MOD) % MOD; return this; } Modint & operator=(const Modint & x) { val = (i128)val x.val % MOD; return this; } Modint & operator^=(ll b) { return this = this ^ b; } Modint & operator^=(const Modint & b) { return this = this ^ b.val; } Modint & operator/=(const Modint & x) { return this = this / x; } Modint & operator++() { return this += Modint(1); } Modint operator++(int) { Modint tmp = this; ++this; return tmp; } Modint & operator--() { return this -= Modint(1); } Modint operator--(int) { Modint tmp = this; --this; return tmp; } bool operator==(const Modint & x) const { return val == x.val; } bool operator!=(const Modint & x) const { return val != x.val; } bool operator<(const Modint & x) const { return val < x.val; } bool operator>(const Modint & x) const { return val > x.val; } bool operator<=(const Modint & x) const { return val <= x.val; } bool operator>=(const Modint & x) const { return val >= x.val; } friend Modint operator+(ll v, const Modint & x) { return Modint(v) + x; } friend Modint operator-(ll v, const Modint & x) { return Modint(v) - x; } friend Modint operator(ll v, const Modint & x) { return Modint(v) x; } friend Modint operator/(ll v, const Modint & x) { return Modint(v) / x; } friend Modint operator^(ll v, const Modint & x) { return Modint(v) ^ x; } friend ostream & operator<<(ostream & os, const Modint & x) { return os << x.val; } friend istream & operator>>(istream & is, Modint & x) { ll v; is >> v; x = Modint(v); return is; } };

const int inf = 1e9; const ll infll = 1e18; const double eps = 1e-8; const ld epsll = 1e-12; const int N = 200005; int la, lb, k, q; set<pair<char, char>> s; string ans[26], a, b; int pre[N]; signed main() { fast_io(); cin >> la >> lb >> k >> q >> a >> b; while (k--) { char u, v; cin >> u >> v; s.insert({u, v}); // 存储「丑陋颜色对」 } rep(i, 0, 25) { // i枚举a到z,也就是0~25 char u = 'a' + i; rep(j, 0, lb - 1) { char v = b[j]; if (!s.count({u, v})) { // 如果不是「丑陋颜色对」 ans[i] += u; ans[i] += v; // 把答案加进去 } } } pre[0] = 0; // 前缀和初始化 rep(i, 1, la) pre[i] = pre[i - 1] + ans[a[i - 1] - 'a'].size(); //前缀和 while (q--) { int x; cin >> x; int idx = upper_bound(pre, pre + la + 1, x) - pre; // 二分,找到第一个大于x的pre[i] --idx; //减去1得到当前段的编号 cout << ans[a[idx] - 'a'][x - pre[idx]] << endl; //输出答案 } return 0; // 完结撒花 }


::::
#### 时间复杂度分析
设字符集大小为 $k$,则时间复杂度为 $O(k × lb + la + q \log la)$,可以通过此题。