QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#373843#850. Edit Distance Yet AgainjijidawangAC ✓3485ms55408kbC++143.6kb2024-04-02 08:51:222024-04-02 08:51:24

Judging History

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

  • [2024-04-02 08:51:24]
  • 评测
  • 测评结果:AC
  • 用时:3485ms
  • 内存:55408kb
  • [2024-04-02 08:51:22]
  • 提交

answer

#include <bits/stdc++.h>
namespace // my guiding star
{
#define filein(x) freopen(x".in", "r", stdin);
#define fileout(x) freopen(x, "w", stdout);
#define fileerr(x) freopen(x, "w", stderr);
#define file(x) freopen(x".in", "r", stdin), freopen(x".out", "w", stdout);
#define files(x) freopen(x".in", "r", stdin), freopen(x".ans", "w", stdout);
using namespace std;
#define cT const T&
template<typename T>
inline T chkmin(T& x, cT y){if (x > y) x = y; return x;}
template<typename T>
inline T chkmax(T& x, cT y){if (x < y) x = y; return x;}
template <typename T>
inline bool inrange(cT x, cT l, cT r){return (l <= x) && (x <= r);}
template <typename T>
inline bool inrange(cT l, cT r, cT L, cT R){return (L <= l) && (r <= R);}
#undef cT
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef unsigned u32;
template <typename T>
using pr = pair<T, T>;
typedef pr<int> pii;
typedef pr<ll> pll;
typedef pr<db> pdd; 
typedef complex<double> cp;
typedef vector<int> vi;
inline void YN(bool x){puts(x ? "Yes" : "No");}
}
const int N = 1e6 + 233, P1 = 1e9 + 7, P2 = 1e9 + 9, base = 131, K = 1005;
int n, m, k;
string s1, s2;
struct hash_t
{
	int first, second;
	inline hash_t operator + (const hash_t& rhs) const {return {(first + rhs.first) % P1, (second + rhs.second) % P2};}
	inline hash_t operator - (const hash_t& rhs) const {return {((first - rhs.first) % P1 + P1) % P1, ((second - rhs.second) % P2 + P2) % P2};}
	inline hash_t operator * (const hash_t& rhs){return {(int)(1ll * first * rhs.first % P1), (int)(1ll * second * rhs.second % P2)};}
	inline bool operator == (const hash_t& rhs) const {return (first == rhs.first) && (second == rhs.second);}
}pw[N], hsh1[N], hsh2[N];
inline hash_t hash1(int l, int r){return hsh1[r] - pw[r-l+1] * hsh1[l-1];}
inline hash_t hash2(int l, int r){return hsh2[r] - pw[r-l+1] * hsh2[l-1];}
struct info
{
	int v, x, y;
	info& operator += (const info& rhs){return (v < rhs.v) ? *this = rhs : *this;}
}dp[K][K << 1];
inline int lcp(int x, int y)
{
	int l = 0, r = min(n-x+1, m-y+1);
	while (l <= r)
	{
		int mid = (l + r) >> 1;
		if (hash1(x, x+mid-1) == hash2(y, y+mid-1)) l = mid + 1;
		else r = mid - 1;
	}
	return l - 1;
}
void print(int p, int x, int y)
{
	printf("YES\n%d\n", p);
	while (p)
	{
		info t = dp[p][y-x+k];
		int tx = t.x, ty = t.y;
		if (y - x == ty - tx) printf("REPLACE %d %c\n", tx+1, s2[ty+1]);
		else if (y - x == ty - tx - 1) printf("DELETE %d\n", tx+1);
		else printf("INSERT %d %c\n", tx+1, s2[ty+1]);
		--p; x = tx; y = ty;
	}
}
int main()
{
	int T; scanf("%d", &T); pw[0] = {1, 1};
	for (int i=1; i<N; i++) pw[i] = pw[i-1] * hash_t{base, base};
	while (T--)
	{
		scanf("%d%d%d", &n, &m, &k);
		cin >> s1 >> s2; s1 = "$" + s1; s2 = "$" + s2;
		for (int i=1; i<=n; i++) hsh1[i] = hsh1[i-1] * hash_t{base, base} + hash_t{s1[i], s1[i]};
		for (int i=1; i<=m; i++) hsh2[i] = hsh2[i-1] * hash_t{base, base} + hash_t{s2[i], s2[i]};
		for (int i=0; i<=k; i++) fill(dp[i], dp[i]+2*k+1, info{-1, 0, 0});
		bool ok = false; dp[0][k] = {lcp(1, 1), 0, 0};
		for (int i=0; (i<=k)&&!ok; i++)
			for (int j=-k; j<=k; j++)
			{
				if (!~dp[i][j+k].v) continue;
				int x = dp[i][j+k].v, y = x + j;
				if ((x == n) && (y == m)){ok = true; print(i, x, y); break;}
				if ((i < k) && (x < n) && (y < m)) dp[i+1][j+k] += info{x+1+lcp(x+2,y+2), x, y};
				if ((i < k) && (x < n) && (j > -k)) dp[i+1][j-1+k] += info{x+1+lcp(x+2,y+1), x, y};
				if ((i < k) && (y < m) && (j < k)) dp[i+1][j+1+k] += info{x+lcp(x+1,y+2), x, y};
			}
		if (!ok) puts("NO");
	}
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 7ms
memory: 12636kb

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: 13ms
memory: 16196kb

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
REPLACE 31 p
REPLACE 29 k
REPLACE 28 c
REPLACE 27 o
REPL...

result:

ok OK!

Test #3:

score: 0
Accepted
time: 2468ms
memory: 36708kb

input:

100
3211 3185 685
wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww...

output:

YES
552
REPLACE 3211 f
REPLACE 3201 b
REPLACE 3198 a
REPLACE 3192 v
REPLACE 3188 g
REPLACE 3181 l
REPLACE 3176 q
REPLACE 3173 j
REPLACE 3168 y
REPLACE 3158 y
REPLACE 3149 m
REPLACE 3147 x
REPLACE 3113 p
REPLACE 3109 n
REPLACE 3108 n
REPLACE 3105 g
REPLACE 3102 g
REPLACE 3096 e
REPLACE 3095 d
REPLACE...

result:

ok OK!

Test #4:

score: 0
Accepted
time: 2879ms
memory: 55408kb

input:

5
999000 998976 995
llllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll...

output:

YES
894
REPLACE 998911 h
REPLACE 996108 x
REPLACE 996096 g
REPLACE 994700 i
REPLACE 994360 u
REPLACE 993105 q
REPLACE 993027 q
REPLACE 992787 w
REPLACE 991955 u
REPLACE 990316 v
REPLACE 989560 d
REPLACE 989480 y
REPLACE 989317 n
REPLACE 988977 d
REPLACE 988162 w
REPLACE 988083 e
REPLACE 985762 v
REP...

result:

ok OK!

Test #5:

score: 0
Accepted
time: 3485ms
memory: 55184kb

input:

5
999000 998971 1000
lllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll...

output:

NO
NO
NO
NO
NO

result:

ok OK!

Test #6:

score: 0
Accepted
time: 869ms
memory: 54372kb

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: 2936ms
memory: 54212kb

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!