QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#394969#850. Edit Distance Yet AgainieeAC ✓2522ms52940kbC++143.0kb2024-04-20 23:34:212024-04-20 23:34:22

Judging History

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

  • [2024-04-20 23:34:22]
  • 评测
  • 测评结果:AC
  • 用时:2522ms
  • 内存:52940kb
  • [2024-04-20 23:34:21]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
constexpr int N = 1e6 + 5, K = 1005;
constexpr int base1 = 31, mod1 = 998244353;
constexpr int base2 = 37, mod2 = 1e9 + 7;
int n, m, k;
char s[N], t[N];
int f[K][K * 2];
pair<int, int> g[K][K * 2];
int s1[N], s2[N];
int t1[N], t2[N];
int pw1[N], pw2[N];
int gets1(int l, int r) { return (s1[r] - 1ll * s1[l - 1] * pw1[r - l + 1] % mod1 + mod1) % mod1; }
int gets2(int l, int r) { return (s2[r] - 1ll * s2[l - 1] * pw2[r - l + 1] % mod2 + mod2) % mod2; }
int gett1(int l, int r) { return (t1[r] - 1ll * t1[l - 1] * pw1[r - l + 1] % mod1 + mod1) % mod1; }
int gett2(int l, int r) { return (t2[r] - 1ll * t2[l - 1] * pw2[r - l + 1] % mod2 + mod2) % mod2; }
int get(int I, int J) {
	int l = I - 1, r = min(n, I + m - J);
	while (l < r) {
		int mid = (l + r + 1) / 2;
		if (gets1(I, mid) == gett1(J, mid - I + J) && gets2(I, mid) == gett2(J, mid - I + J)) l = mid;
		else r = mid - 1;
	}
	return l;
}
void trans(int r, int ld, int nd, int lv, int v) {
	if (f[r + 1][nd + k] < v) {
		f[r + 1][nd + k] = v;
		g[r + 1][nd + k] = {ld, lv};
	}
}
void solve() {
	cin >> n >> m >> k;
	for (int i = 1; i <= n; i++) {
		cin >> s[i];
	}
	for (int i = 1; i <= m; i++) {
		cin >> t[i];
	}
	if (abs(n - m) > k) {
		cout << "NO" << "\n";
		return;
	}
	for (int i = 1; i <= n; i++) {
		s1[i] = (1ll * s1[i - 1] * base1 + (s[i] - 'a' + 1)) % mod1;
		s2[i] = (1ll * s2[i - 1] * base2 + (s[i] - 'a' + 1)) % mod2;
	}
	for (int i = 1; i <= m; i++) {
		t1[i] = (1ll * t1[i - 1] * base1 + (t[i] - 'a' + 1)) % mod1;
		t2[i] = (1ll * t2[i - 1] * base2 + (t[i] - 'a' + 1)) % mod2;
	}
	memset(f, ~0x3f, sizeof f);
	f[0][0 + k] = get(0, 0);
	for (int r = 0; r < k; r++) {
		for (int d = -k; d <= k; d++) {
			if (f[r][d + k] < 0) continue;
			int i = f[r][d + k], j = i + d;
			if (i < n && j < m) trans(r, d, d, i, get(i + 2, j + 2));
			if (i < n) trans(r, d, d - 1, i, get(i + 2, j + 1));
			if (j < m) trans(r, d, d + 1, i, get(i + 1, j + 2));
		}
	}
	int fir = -1;
	for (int r = 0; r <= k; r++) {
		if (f[r][m - n + k] == n) {
			fir = r;
			break;
		}
	}
	if (fir == -1) {
		cout << "NO" << "\n";
		return;
	}
	vector<array<int, 3>> v;
	v.push_back({n, m, m - n});
	for (int r = fir, d = m - n; r; r--) {
		int i = g[r][d + k].second;
		d = g[r][d + k].first;
		int j = i + d;
		v.push_back({i, j, d});
	}
	cout << "YES" << "\n" << fir << "\n";
	reverse(v.begin(), v.end());
	int pre = 0;
	for (int id = 0; id + 1 < v.size(); id++) {
		auto [i, j, d] = v[id];
		int diff = v[id + 1][2] - d;
		if (diff == -1) cout << "DELETE" << " " << i + 1 + pre << "\n", pre--;
		else if (diff == 0) cout << "REPLACE" << " " << i + 1 + pre << " " << t[j + 1] << "\n";
		else cout << "INSERT" << " " << i + pre + 1 << " " << t[j + 1] << "\n", pre++;
	}
}
int main() {
	cin.tie(0)->sync_with_stdio(0);
	for (int i = pw1[0] = pw2[0] = 1; i < N; i++) {
		pw1[i] = 1ll * pw1[i - 1] * base1 % mod1;
		pw2[i] = 1ll * pw2[i - 1] * base2 % mod2;
	}
	int T;
	cin >> T;
	while (T--) {
		solve();
	}
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 2ms
memory: 22096kb

input:

2
3 4 3
kot
plot
5 7 3
zycie
porazka

output:

YES
2
REPLACE 1 p
INSERT 2 l
NO

result:

ok OK!

Test #2:

score: 0
Accepted
time: 30ms
memory: 24568kb

input:

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

output:

YES
0
YES
16
DELETE 1
DELETE 1
DELETE 1
DELETE 1
DELETE 1
DELETE 1
DELETE 1
DELETE 1
DELETE 1
REPLACE 1 q
REPLACE 2 y
REPLACE 3 d
REPLACE 4 o
REPLACE 5 p
REPLACE 6 a
REPLACE 7 q
YES
28
DELETE 1
DELETE 1
DELETE 1
DELETE 1
REPLACE 1 o
REPLACE 2 s
REPLACE 3 k
REPLACE 5 m
REPLACE 6 l
REPLACE 8 j
REPLACE...

result:

ok OK!

Test #3:

score: 0
Accepted
time: 2372ms
memory: 36828kb

input:

100
3211 3185 685
wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww...

output:

YES
552
DELETE 4
DELETE 4
DELETE 4
DELETE 4
DELETE 4
DELETE 4
DELETE 4
DELETE 4
DELETE 4
DELETE 4
DELETE 4
DELETE 4
DELETE 4
DELETE 4
DELETE 4
DELETE 4
DELETE 4
DELETE 4
DELETE 4
DELETE 4
DELETE 4
DELETE 4
DELETE 4
DELETE 4
DELETE 4
DELETE 4
REPLACE 4 i
REPLACE 16 b
REPLACE 17 s
REPLACE 20 u
REPLACE...

result:

ok OK!

Test #4:

score: 0
Accepted
time: 2239ms
memory: 52740kb

input:

5
999000 998976 995
llllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll...

output:

YES
894
DELETE 1682
DELETE 1682
DELETE 1682
DELETE 1682
DELETE 1682
DELETE 1682
DELETE 1682
DELETE 1682
DELETE 1682
DELETE 1682
DELETE 1682
DELETE 1682
DELETE 1682
DELETE 1682
DELETE 1682
DELETE 1682
DELETE 1682
DELETE 1682
DELETE 1682
DELETE 1682
DELETE 1682
DELETE 1682
DELETE 1682
DELETE 1682
REPL...

result:

ok OK!

Test #5:

score: 0
Accepted
time: 2522ms
memory: 52908kb

input:

5
999000 998971 1000
lllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll...

output:

NO
NO
NO
NO
NO

result:

ok OK!

Test #6:

score: 0
Accepted
time: 1057ms
memory: 52940kb

input:

5
1000000 1000000 1000
faafaffaaffafaafaffafaaffaafaffaaffafaaffaafaffafaafaffaaffafaafaffafaaffaafaffafaafaffaaffafaaffaafaffaaffafaafaffafaaffaafaffaaffafaaffaafaffafaafaffaaffafaaffaafaffaaffafaafaffafaaffaafaffafaafaffaaffafaafaffafaaffaafaffaaffafaaffaafaffafaafaffaaffafaafaffafaaffaafaffafaafa...

output:

YES
246
DELETE 1
REPLACE 2 f
DELETE 6
DELETE 11
DELETE 12
DELETE 21
DELETE 22
DELETE 23
DELETE 24
DELETE 41
DELETE 42
DELETE 43
DELETE 44
DELETE 45
DELETE 46
DELETE 47
DELETE 48
DELETE 81
REPLACE 82 f
DELETE 86
INSERT 91 a
REPLACE 94 f
INSERT 97 a
DELETE 113
REPLACE 114 a
DELETE 118
INSERT 123 f
REP...

result:

ok OK!

Test #7:

score: 0
Accepted
time: 1940ms
memory: 52744kb

input:

5
999900 999925 992
ejqwvowvntrtmokrfeqeybcvgsupftozhkdsyytrioxrzbahikexefgyhpwbcztcydqiibxukojvgedldgmjufkzwttpvftctkpajgpwiklfzrkjxzsltqdbfdyxqpswrrlkdbmwckbhbfpqqgnhcixcnmphjulyykqfvdrqsdskbxqlqvekhwmvzmtqannmxrjgqcpdpofmqozyicaiqjikqxkeolandjydgvtjhnzubpdtkxymaxffnopnnrpnedobnfljcvtqwmuxdctutidm...

output:

YES
938
DELETE 1984
DELETE 2243
INSERT 4441 b
DELETE 5841
DELETE 6398
INSERT 6785 b
REPLACE 6948 t
INSERT 6988 g
REPLACE 7101 b
INSERT 7373 b
DELETE 10782
INSERT 12028 g
REPLACE 13721 g
INSERT 14908 t
DELETE 14925
INSERT 15582 b
REPLACE 17831 b
INSERT 19811 b
DELETE 22361
REPLACE 22445 t
INSERT 2508...

result:

ok OK!