QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#367702 | #850. Edit Distance Yet Again | edisnimorF | AC ✓ | 1215ms | 78864kb | C++14 | 2.9kb | 2024-03-26 12:01:40 | 2024-03-26 12:01:42 |
Judging History
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=1005;
const int P=19260817, M1=998244353, M2=1e9+7;
ll pw1[N], pw2[N];
struct Hsh{
ll h1[N], h2[N];
void init(char *f, int len){
for(int i=1; i<=len; i++){
h1[i]=(h1[i-1]*P+f[i]-'a'+1)%M1;
h2[i]=(h2[i-1]*P+f[i]-'a'+1)%M2;
}
}
il pii que(int l, int r){
return {
(h1[r]-h1[l-1]*pw1[r-l+1]%M1+M1)%M1,
(h2[r]-h2[l-1]*pw2[r-l+1]%M2+M2)%M2
};
}
} hs, ht;
int n, m, K, f[1005][2010], dt[1005][2010];
pii g[1005][2010];
char s[N], t[N];
int calc(int i, int j){
int l=0, r=min(n-i, m-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 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);
f[0][Mov]=0;
auto upd=[&](int i, int j, int s, int ii, int jj) {if(f[i][j]<s) f[i][j]=s, g[i][j]={ii, jj};};
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=dt[i][j+Mov]=calc(s, s+j);
f[i][j+Mov]+=d, s+=d;
if(s<n) upd(i+1, j-1+Mov, s+1, i, j+Mov);//删除 s(s+d+1)
if(s+j<m) upd(i+1, j+1+Mov, s, i, j+Mov);//在 s(s+d) 后添加字符
if(s<n && s+j<m) upd(i+1, j+Mov, s+1, i, j+Mov);//修改 s(s+d+1)
}
}
for(int i=0; i<=K; i++) if(f[i][m-n+Mov]==n){
printf("YES\n%d\n", i);
int x=i, y=m-n+Mov;
for(int j=1; j<=i; j++){
auto [xx, yy]=g[x][y];
n=f[x][y]-dt[x][y], m=f[x][y]+y-Mov-dt[x][y];
if(yy>y) printf("DELETE %d\n", n);
else if(yy<y) printf("INSERT %d %c\n", n+1, t[m]);
else printf("REPLACE %d %c\n", n, t[m]);
x=xx, y=yy;
}
return;
}
puts("NO");
// printf("YES\n%d\n", mp[{n, m}].fr);
// while(n || m){
// auto[i, j]=mp[{n, m}];
// if(g[i][j]==0){
// printf("DELETE %d\n", n);
// n--;
// }
// else if(g[i][j]==1){
// printf("INSERT %d %c\n", n+1, t[m]);
// m--;
// }
// else{
// printf("REPLACE %d %c\n", n, t[m]);
// n--; m--;
// }
// while(n && m && s[n]==t[m]) n--, m--;
// }
}
char _end;
signed main(){
debug("%lfMB\n", (&_end-&_begin)/1024./1024.);
pw1[0]=1; for(int i=1; i<=1e6; i++) pw1[i]=pw1[i-1]*P%M1;
pw2[0]=1; for(int i=1; i<=1e6; i++) pw2[i]=pw2[i-1]*P%M2;
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: 8ms
memory: 27436kb
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: 39ms
memory: 28248kb
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: 874ms
memory: 45804kb
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: 974ms
memory: 78780kb
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: 1106ms
memory: 78812kb
input:
5 999000 998971 1000 lllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll...
output:
NO NO NO NO NO
result:
ok OK!
Test #6:
score: 0
Accepted
time: 430ms
memory: 78864kb
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: 1215ms
memory: 78744kb
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!