QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#367616#850. Edit Distance Yet AgainedisnimorFML 7ms21608kbC++142.6kb2024-03-26 10:39:362024-03-26 10:39:37

Judging History

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

  • [2024-03-26 10:39:37]
  • 评测
  • 测评结果:ML
  • 用时:7ms
  • 内存:21608kb
  • [2024-03-26 10:39:36]
  • 提交

answer

#include<bits/stdc++.h>
#define il inline
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define pii pair<int, int>
#define fr first
#define sc second
#define ll long long
#define mset(f, z) memset(f, z, sizeof(f))
#define mcpy(f, g) memcpy(f, g, sizeof(g))
using namespace std;
template<typename T=ll>
il T rd(){
	T s=0; bool f=1; char c=getchar();
	while(!isdigit(c)) f^=(c=='-'), c=getchar();
	while(isdigit(c)) s=s*10+c-'0', c=getchar();
	return f? s:-s;
}
template<typename T> il void ckmx(T &x, T y){if(x<y) x=y;}
template<typename T> il void ckmn(T &x, T y){if(y<x) x=y;}
char _begin;

const int N=1e6+5, Mov=1000;
const int P=10, Mod=20070831;

ll pw[N];

struct Hsh{
	ll hsh[N];

	void init(char *f, int len){
		for(int i=1; i<=len; i++) hsh[i]=(hsh[i-1]*P+f[i]-'a'+1)%Mod;
	}

	il ll que(int l, int r){return (hsh[r]-hsh[l-1]*pw[r-l+1]%Mod+Mod)%Mod;}
} hs, ht;


int n, m, K, f[1005][2005], g[1005][2005];
map<pii, pii> mp;
char s[N], t[N];

int calc(int i, int j){
	int l=0, r=min(n-i, n-j)+1;
	while(r-l>1){
		int mid=(l+r)/2;
		(hs.que(i+1, i+mid)==ht.que(j+1, j+mid)) ? l=mid : r=mid;
	}
	return l;
}

void upd(int i, int j, int s, int typ){
	if(f[i][j]<s){
		f[i][j]=s;
		g[i][j]=typ;
	}
}

void solve(){
	n=rd(), m=rd(), K=rd();
	scanf("%s%s", s+1, t+1);
	if(abs(n-m)>K){
		puts("NO");
		return;
	}
	hs.init(s, n);
	ht.init(t, m);
	mset(f, ~63);
	map<pii, pii>().swap(mp);
	f[0][Mov]=0;
	for(int i=0; i<K; i++){
		for(int j=-K; j<=K; j++) if(f[i][j+Mov]>=0){
			int s=f[i][j+Mov];
			int d=calc(s, s+j);
			if(s+d<n) upd(i+1, j-1+Mov, s+d+1, 0);//删除 s(s+d+1)
			if(s+j+d<m) upd(i+1, j+1+Mov, s+d, 1);//在 s(s+d) 后添加字符 
			if(s+d<n && s+j+d<m) upd(i+1, j+Mov, s+d+1, 2);//修改 s(s+d+1)
		}
	}
	for(int i=1; i<=K; i++){
		for(int j=-K; j<=K; j++) if(f[i][j+Mov]>=0){
			int s=f[i][j+Mov];
			if(!mp.count({s, s+j})) mp[{s, s+j}]={i, j+Mov};
		}
	}
	int x=n, y=m;
	while(x && y && s[x]==t[y]) x--, y--;
	if(!x && !y){
		printf("YES\n0\n");
		return;
	}
	if(!mp.count({x, y})){
		puts("NO");
		return;
	}
	printf("YES\n%d\n", mp[{x, y}].fr);
	while(x || y){
		auto[i, j]=mp[{x, y}];
		if(g[i][j]==0){
			printf("DELETE %d\n", x);
			x--;
		}
		else if(g[i][j]==1){
			printf("INSERT %d %c\n", x+1, t[y]);
			y--;
		}
		else{
			printf("REPLACE %d %c\n", x, t[y]);
			x--; y--;
		}
		while(x && y && s[x]==t[y]) x--, y--;
	}
}

char _end;
signed main(){
	debug("%lfMB\n", (&_end-&_begin)/1024./1024.);
	pw[0]=1;
	for(int i=1; i<=1e6; i++) pw[i]=pw[i-1]*P%Mod;
	int T=rd();
	while(T--) solve();
	return 0;
}
/*
1
3 3 100
bbb
abc
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: -100
Memory Limit Exceeded

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 31
DELETE 30
DELETE 29
DELETE 28
REPLACE 27 p
REP...

result: