North America Championship 2020(XX Open Cup,GP of North America) Editing Explosion

题目描述

https://vjudge.net/problem/Kattis-editingexplosion

Solution

我们首先考虑如何求编辑距离,可以轻松得到如下的 $dp$ 式子

1
2
3
4
5
6
7
for (int i = 1; i <= n; ++i) { 
f[i][0] = i;
for (int j = 1; j <= m; ++j) {
if (s[i] == t[j]) f[i][j] = f[i - 1][j - 1];
else f[i][j] = min(f[i - 1][j - 1], f[i - 1][j], f[i][j - 1]);
}
}

那么与hero meet devil那题类似,我们考虑将这个东西状压,不过经过思考之后能够发现这东西没法状压,那么我们就干脆一点直接把这个东西扔到 $map$ 里

未知时间复杂度,反正能过

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <cstring>
#include <algorithm>
#include <map>
#include <vector>
#define maxn 21
#define maxm 11
using namespace std;

const int p = 998244353;

int n, m, d, a[maxm];

char s[maxm];

map<vector<int>, int> f[maxn];
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);

cin >> s + 1 >> d; m = strlen(s + 1); n = m + d;
for (int i = 1; i <= m; ++i) a[i] = s[i] - 'A';
vector<int> tmp(m + 1); for (int i = 0; i <= m; ++i) tmp[i] = i;
f[0][tmp] = 1;
for (int i = 0; i < n; ++i)
for (auto [s, v] : f[i]) {
if (*min_element(s.begin(), s.end()) > d) continue;
for (int k = 0; k < 26; ++k) {
vector<int> t(m + 1); t[0] = i + 1;
for (int i = 1; i <= m; ++i) {
if (a[i] == k) t[i] = s[i - 1];
else t[i] = min({ t[i - 1], s[i - 1], s[i] }) + 1;
}
f[i + 1][t] = (f[i + 1][t] + v) % p;
}
}
int ans = 0;
for (int i = 0; i <= n; ++i)
for (auto [vec, val] : f[i])
if (vec[m] == d) ans = (ans + val) % p;
cout << ans << "\n";
return 0;
}