QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#217348 | #6366. Message | luanmenglei | WA | 2ms | 13912kb | C++17 | 3.4kb | 2023-10-16 19:44:53 | 2023-10-16 19:44:54 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
void debug(const char *msg, ...) {
#ifdef CLESIP
va_list arg;
static char pbString[512];
va_start(arg,msg);
vsprintf(pbString,msg,arg);
cerr << "[DEBUG] " << pbString << "\n";
va_end(arg);
#endif
}
using u64 = unsigned long long;
using i64 = long long;
using i128 = __int128_t;
template <typename T, typename L> void chkmax(T &x, L y) { if (x < y) x = y; }
template <typename T, typename L> void chkmin(T &x, L y) { if (x > y) x = y; }
const int N = 2e5 + 10;
const int W = 26;
const int K = 70;
const i64 INF = 1e18;
const u64 B = 131;
int n, m, a[N], apr[2][W], b[K], cur, id[N], nms[N], nxt[N];
bool must[W + 1];
i64 f[N][K], sum[N];
u64 pw[N * 2], hsh[N];
char s[N], t[N], subs[N];
void calc_hash() {
for (int i = 1; i <= cur; i ++) {
hsh[i] = hsh[i - 1] + pw[i] * (subs[i] - 'a' + 77);
}
}
u64 get_hash(int l, int r) {
return (hsh[r] - hsh[l - 1]) * pw[cur - l];
}
void solve() {
cin >> n >> m >> (s + 1) >> (t + 1);
for (int i = 1; i <= n; i ++)
cin >> a[i];
for (int i = 0; i < W; i ++)
apr[0][i] = apr[1][i] = 0;
for (int i = 1; i <= m; i ++) {
if (!apr[0][t[i] - 'a'])
apr[0][t[i] - 'a'] = i;
apr[1][t[i] - 'a'] = i;
}
int cnt = 0;
for (int i = 0; i < W; i ++) if (apr[0][i])
b[++ cnt] = apr[0][i], b[++ cnt] = apr[1][i];
b[++ cnt] = 1, b[++ cnt] = m + 1;
sort(b + 1, b + 1 + cnt);
cnt = unique(b + 1, b + 1 + cnt) - b - 1;
for (int i = 0; i <= n; i ++)
for (int j = 0; j <= cnt; j ++)
f[i][j] = INF;
f[0][0] = 0;
s[0] = 'a' + 1;
for (int j = 0; j < cnt; j ++) {
int l = b[j], r = b[j + 1] - 1, len = r - l;
// debug("current range (%d, %d)", l, r);
for (int i = 0; i < W; i ++)
must[i] = (apr[0][i] <= l && l < apr[1][i]);
if (j > 0) {
cur = 0;
for (int i = 1; i <= n; i ++) {
sum[i] = sum[i - 1];
if (must[s[i] - 'a']) {
subs[++ cur] = s[i];
id[cur] = i;
} else {
sum[i] += a[i];
}
}
nms[n + 1] = n + 1, nxt[n + 1] = n + 1;
for (int i = n; i >= 0; i --) {
nms[i] = nms[i + 1], nxt[i] = nxt[i + 1];
if (must[s[i] - 'a'])
nms[i] = i;
if (s[i] == t[l])
nxt[i] = i;
}
calc_hash();
u64 self = 0;
for (int i = l + 1; i <= r; i ++)
self += pw[cur + i - l - 1] * (t[i] - 'a' + 77);
for (int i = 0, pos = 1; i < n; i ++) if (nms[i + 1] >= nxt[i + 1] && nxt[i + 1] <= n) {
// debug("???? i: %d j: %d f: %lld", i, j, f[i][j - 1]);
int nxt_trans = nxt[i + 1];
if (len > 0) {
while (pos <= cur && id[pos] <= nxt_trans)
++ pos;
if (pos + len - 1 > cur || get_hash(pos, pos + len - 1) != self)
continue;
nxt_trans = id[pos + len - 1];
}
// debug("nxt_trans: %d", nxt_trans);
chkmin(f[nxt_trans][j], f[i][j - 1] + sum[nxt_trans] - sum[i] - (!must[t[l] - 'a'] ? a[nxt[i + 1]] : 0));
// debug("f[nxt_trans]: %lld", f[nxt_trans][j]);
}
}
for (int i = 1; i <= n; i ++) if (!must[s[i] - 'a']) {
chkmin(f[i][j], f[i - 1][j] + a[i]);
// debug("???? f[%d][%d] = %lld", i, j, f[i][j]);
}
}
if (f[n][cnt - 1] == INF)
cout << "-1\n";
else
cout << f[n][cnt - 1] << "\n";
}
int main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
pw[0] = 1;
for (int i = 1; i < N * 2; i ++)
pw[i] = pw[i - 1] * B;
solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 2ms
memory: 13912kb
input:
ababccb abc 7 2 2 4 3 2 1
output:
0
result:
wrong answer 1st lines differ - expected: '7', found: '0'