QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#368599#850. Edit Distance Yet AgainJerryTclAC ✓2534ms38128kbC++202.4kb2024-03-27 13:52:492024-03-27 13:52:49

Judging History

你现在查看的是最新测评结果

  • [2024-03-27 13:52:49]
  • 评测
  • 测评结果:AC
  • 用时:2534ms
  • 内存:38128kb
  • [2024-03-27 13:52:49]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
using uint64 = unsigned long long;
void Solve() {
    int n, m, k; cin >> n >> m >> k;
    string s, t; cin >> s >> t;
    if(abs(n - m) > k) return puts("NO"), void();
    vector<uint64> hshs(n + 1), hsht(m + 1);
    vector<uint64> pw(max(n, m) + 1); pw[0] = 1;
    const int B = 137; const uint64 Mod = 1e17l + 7;
    const auto Hsh = [&](const vector<uint64> &h, int p, int q)
        { return (h[p + q] + (__uint128_t)(Mod - h[p]) * pw[q]) % Mod; };
    for(int i = 0; i < max(n, m); ++i) pw[i + 1] = pw[i] * B % Mod;
    for(int i = 0; i < n; ++i) hshs[i + 1] = (hshs[i] * B + s[i] - 'a') % Mod;
    for(int i = 0; i < m; ++i) hsht[i + 1] = (hsht[i] * B + t[i] - 'a') % Mod;
    const auto &match = [&](int x, int ofs) {
        const int y = x + ofs; int A = x;
        if(x > n || y > m) return 0;
        for(int L = x + 1, R = n; L <= R; ) {
            const int M = (L + R) >> 1, e = M - x;
            const uint64 u = Hsh(hshs, x, e), v = Hsh(hsht, y, e);
            if(u == v) L = M + 1, A = M; else R = M - 1;
        }
        return A;
    };
    using pii = pair<int, int>; vector<vector<pii>> f;
    f.emplace_back(1, pii(match(0, 0), 0));
    const auto &Cmax = [](pii &x, pii y) { x = max(x, y); };
    const auto &check = [&](int p) {
        if(f[p][m - n + p].first != n) return 0;
        printf("YES\n%d\n", p);
        for(int w = m - n; p > 0; --p) {
            const int V = f[p][w + p].second;
            const int u = V >> 2, v = u + 1, ofs = V & 3;
            switch(ofs) {
                case 0: printf("DELETE %d\n", v), ++w; break;
                case 1: printf("REPLACE %d %c\n", v, t[u + w]); break;
                case 2: printf("INSERT %d %c\n", v, t[u + --w]); break;
            }
        }
        return 1;
    };
    for(int p = 0, q = 1;; ++p, ++q) {
        if(abs(n - m) <= p && check(p)) return;
        if(p == k) return puts("NO"), void();
        f.emplace_back(q << 1 | 1, pii(0, 0));
        for(int i = 0; i <= p * 2; ++i) {
            const int s = f[p][i].first, t = s << 2;
            Cmax(f[q][i + 0], {match(s + 1, i - q), t | 0});
            Cmax(f[q][i + 1], {match(s + 1, i - p), t | 1});
            Cmax(f[q][i + 2], {match(s, i - p + 1), t | 2});
        }
    }
}
int main() {
    cin.tie(0)->sync_with_stdio(0);
    int T; cin >> T; while(T--) Solve();
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 3740kb

input:

2
3 4 3
kot
plot
5 7 3
zycie
porazka

output:

YES
2
INSERT 2 l
REPLACE 1 p
NO

result:

ok OK!

Test #2:

score: 0
Accepted
time: 4ms
memory: 4044kb

input:

100
100 100 0
mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
16 7 100
vvvvvvvvvvvvvvvv
qydopaq
37 33 43
ggggggggggggggggggggggggggggggggggggg
osk...

output:

YES
0
YES
16
REPLACE 16 q
REPLACE 15 a
REPLACE 14 p
REPLACE 13 o
REPLACE 12 d
REPLACE 11 y
REPLACE 10 q
DELETE 9
DELETE 8
DELETE 7
DELETE 6
DELETE 5
DELETE 4
DELETE 3
DELETE 2
DELETE 1
YES
28
REPLACE 37 o
REPLACE 36 o
REPLACE 35 a
REPLACE 34 a
DELETE 33
DELETE 32
DELETE 31
DELETE 30
REPLACE 27 p
REP...

result:

ok OK!

Test #3:

score: 0
Accepted
time: 1629ms
memory: 9488kb

input:

100
3211 3185 685
wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww...

output:

YES
552
REPLACE 3211 f
DELETE 3210
DELETE 3209
DELETE 3208
DELETE 3207
DELETE 3206
DELETE 3205
DELETE 3204
DELETE 3203
DELETE 3202
DELETE 3201
DELETE 3200
DELETE 3199
DELETE 3198
DELETE 3197
DELETE 3196
DELETE 3195
DELETE 3194
DELETE 3193
DELETE 3192
DELETE 3191
DELETE 3190
DELETE 3189
DELETE 3188
D...

result:

ok OK!

Test #4:

score: 0
Accepted
time: 2134ms
memory: 36208kb

input:

5
999000 998976 995
llllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll...

output:

YES
894
DELETE 999000
DELETE 998999
DELETE 998998
DELETE 998997
DELETE 998996
DELETE 998995
DELETE 998994
DELETE 998993
DELETE 998992
DELETE 998991
DELETE 998990
DELETE 998989
DELETE 998988
DELETE 998987
DELETE 998986
DELETE 998985
DELETE 998984
DELETE 998983
DELETE 998982
DELETE 998981
DELETE 99898...

result:

ok OK!

Test #5:

score: 0
Accepted
time: 2534ms
memory: 37132kb

input:

5
999000 998971 1000
lllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll...

output:

NO
NO
NO
NO
NO

result:

ok OK!

Test #6:

score: 0
Accepted
time: 569ms
memory: 34376kb

input:

5
1000000 1000000 1000
faafaffaaffafaafaffafaaffaafaffaaffafaaffaafaffafaafaffaaffafaafaffafaaffaafaffafaafaffaaffafaaffaafaffaaffafaafaffafaaffaafaffaaffafaaffaafaffafaafaffaaffafaaffaafaffaaffafaafaffafaaffaafaffafaafaffaaffafaafaffafaaffaafaffaaffafaaffaafaffafaafaffaaffafaafaffafaaffaafaffafaafa...

output:

YES
246
INSERT 2049 f
REPLACE 2047 a
INSERT 2045 f
INSERT 2042 f
INSERT 2041 a
INSERT 2036 f
INSERT 2035 a
INSERT 2034 a
INSERT 2033 f
INSERT 2024 f
INSERT 2023 a
INSERT 2022 a
INSERT 2021 f
INSERT 2020 a
INSERT 2019 f
INSERT 2018 f
INSERT 2017 a
INSERT 1970 f
REPLACE 1967 a
INSERT 1965 f
DELETE 195...

result:

ok OK!

Test #7:

score: 0
Accepted
time: 2161ms
memory: 38128kb

input:

5
999900 999925 992
ejqwvowvntrtmokrfeqeybcvgsupftozhkdsyytrioxrzbahikexefgyhpwbcztcydqiibxukojvgedldgmjufkzwttpvftctkpajgpwiklfzrkjxzsltqdbfdyxqpswrrlkdbmwckbhbfpqqgnhcixcnmphjulyykqfvdrqsdskbxqlqvekhwmvzmtqannmxrjgqcpdpofmqozyicaiqjikqxkeolandjydgvtjhnzubpdtkxymaxffnopnnrpnedobnfljcvtqwmuxdctutidm...

output:

YES
938
INSERT 999192 g
DELETE 999157
REPLACE 998754 g
INSERT 998259 b
INSERT 998188 g
DELETE 998145
REPLACE 997378 t
DELETE 997088
INSERT 994695 t
REPLACE 993971 b
DELETE 990472
REPLACE 988552 t
DELETE 987640
DELETE 985945
DELETE 985429
DELETE 983806
INSERT 983299 t
REPLACE 981534 g
INSERT 978807 t...

result:

ok OK!