QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#251064#850. Edit Distance Yet Againthomas_li#AC ✓927ms74028kbC++173.8kb2023-11-14 09:09:582023-11-14 09:09:58

Judging History

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

  • [2023-11-14 09:09:58]
  • 评测
  • 测评结果:AC
  • 用时:927ms
  • 内存:74028kb
  • [2023-11-14 09:09:58]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll
#define rep(i,a,b) for(int i = a; i < (b); i++)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define PB push_back
#define FS first
#define SD second
#define cmx(x,y) x = max(x,y)
#define cmn(x,y) x = min(x,y)
#define ary(k) array<int,k>
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef uint64_t ull;
struct H{
	ull x; H(ull x=0) : x(x) {}
	H operator+(H o) { return x + o.x + (x+o.x < x);}
	H operator-(H o) { return *this + ~o.x; }
	H operator*(H o) { auto m = (__uint128_t)x * o.x;
		return H((ull)m)+(ull)(m >> 64); }
	ull get() const { return x + !~x; }
	bool operator==(H o) const { return get() == o.get(); }
	bool operator<(H o) const { return get() < o.get(); }
};
static const H C = (ll)1e11+3;
struct HashInterval{
	vector<H> ha,pw;
	HashInterval(string& str) : ha(sz(str)+1),pw(ha) {
		pw[0] = 1;
		rep(i,0,sz(str))
			ha[i+1] = ha[i]*C+str[i],pw[i+1] = pw[i]*C;
	}
	H hashInterval(int a, int b){
		return ha[b]-ha[a]*pw[b-a];
	}
};
int dp[2006][2006];
pii from[2006][2006];
/*
int longest(string &sa, string &sb, int a, int b) {
    int res = 0;
    while (sa+res < sz(sa) && sb+res < sz(sb) && sa[a+res] == sb[b+res]) {
        res++;
    }
    return res;
}*/

void fun() {
    int n, m, k;
    cin >> n >> m >> k;
    string sa, sb;
    cin >> sa >> sb;
	HashInterval ha(sa),hb(sb);
	auto longest = [&](int a, int b){
		int lena = n-a,lenb = m-b;	
		auto chk = [&](int l){
			if(l == 0) return true;
			auto x = ha.hashInterval(a,a+l),y = hb.hashInterval(b,b+l);
			return x == y;
		};
		int lo = 0, hi = min(lena,lenb);
		while(lo < hi){
			int mid = (lo+hi+1)/2;
			if(chk(mid)) lo = mid;
			else hi = mid-1;
		}
		return lo;
	};
    for (int i = 0; i <= k; i++) {
        for (int j = 0; j <= 2*k+2; j++) {
            dp[i][j] = -1000000001;
        }
    }
    dp[0][k+1] = 0;
    for (int i = 0; i <= k; i++) {
        for (int d = 1; d <= 2*k+1; d++) {
            int a = dp[i][d], b = a + d - k - 1;
            if (a < 0 || b < 0)
                continue;
            dp[i][d] += longest(a, b);
            if (dp[i+1][d] < dp[i][d] + 1) {
                from[i+1][d] = {i, d};
                dp[i+1][d] = dp[i][d] + 1;
            }
            if (dp[i+1][d+1] < dp[i][d]) {
                from[i+1][d+1] = {i, d};
                dp[i+1][d+1] = dp[i][d];
            }
            if (dp[i+1][d-1] < dp[i][d] + 1) {
                from[i+1][d-1] = {i, d};
                dp[i+1][d-1] = dp[i][d] + 1;
            }
        }
    }
    pair<int, int> best = {1000000, 0};
    for (int i = 0; i <= k; i++) {
        for (int d = 0; d <= 2*k+2; d++) {
            int a = dp[i][d], b = a + d - k - 1;
            if (a == n && b == m) {
                best = min(best, {i, d});
            }
//            cerr << i << " " << d << " " << a << " " << b << " " << dp[i][d] << "\n";
        }
    }
    if (best.FS == 1000000) {
        cout << "NO\n";
        return;
    }
    cout << "YES\n";
    cout << best.FS << "\n";
    while (best.FS > 0) {
        auto [i, d] = best;
        int dell = false;
        if (from[i][d].SD == d+1) {
            cout << "DELETE ";
            dell = true;
        } else if (from[i][d].SD == d - 1) {
            cout << "INSERT ";
        } else {
            cout << "REPLACE ";
        }
        best = from[i][d];
        i = best.FS, d = best.SD;
        int a = dp[i][d], b = a + d - k - 1;
        cout << a+1;
        if (!dell) {
            cout << " " << sb[b];
        }
        cout << "\n";
    }
}

signed main(){
	cin.tie(0)->sync_with_stdio(0); cin.exceptions(cin.failbit);
    int z;
    cin >> z;
    while (z--) fun();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 0ms
memory: 4732kb

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: 810ms
memory: 46112kb

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: 817ms
memory: 73152kb

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: 795ms
memory: 74028kb

input:

5
999000 998971 1000
lllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll...

output:

NO
NO
NO
NO
NO

result:

ok OK!

Test #6:

score: 0
Accepted
time: 288ms
memory: 73892kb

input:

5
1000000 1000000 1000
faafaffaaffafaafaffafaaffaafaffaaffafaaffaafaffafaafaffaaffafaafaffafaaffaafaffafaafaffaaffafaaffaafaffaaffafaafaffafaaffaafaffaaffafaaffaafaffafaafaffaaffafaaffaafaffaaffafaafaffafaaffaafaffafaafaffaaffafaafaffafaaffaafaffaaffafaaffaafaffafaafaffaaffafaafaffafaaffaafaffafaafa...

output:

YES
246
DELETE 2049
REPLACE 2046 a
DELETE 2043
DELETE 2039
DELETE 2037
DELETE 2031
DELETE 2029
DELETE 2027
DELETE 2025
DELETE 2015
DELETE 2013
DELETE 2011
DELETE 2009
DELETE 2007
DELETE 2005
DELETE 2003
DELETE 2001
DELETE 1953
REPLACE 1950 a
DELETE 1947
INSERT 1942 f
REPLACE 1938 a
INSERT 1937 f
DEL...

result:

ok OK!

Test #7:

score: 0
Accepted
time: 927ms
memory: 73816kb

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!