QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#426167 | #850. Edit Distance Yet Again | lcs080206 | WA | 742ms | 74440kb | C++14 | 2.3kb | 2024-05-30 21:54:34 | 2024-05-30 21:54:35 |
Judging History
answer
#include<bits/stdc++.h>
#define base 1000000007
#define base2 45279
#define ull unsigned long long
#define N 1000005
#define K 1005
using namespace std;
int n,m,k;
int s1[N],s2[N];
ull hash1[N],hash2[N],pw[N];
int dp[K][K<<1],tr[K][K<<1];
ull pre1[N],pre2[N],pw2[N];
bool check(int x,int y,int len){
if((x+len-1>n)||(y+len-1>m))return 0;
if(hash1[x+len-1]-hash1[x-1]*pw[len]==hash2[y+len-1]-hash2[y-1]*pw[len])return 1;
return 0;
}
bool precheck(int x,int y,int len){
if((x+len-1>n)||(y+len-1>m))return 0;
if(pre1[x+len-1]-pre1[x-1]*pw2[len]==pre2[y+len-1]-pre2[y-1]*pw2[len])return 1;
return 0;
}
int sol(int x,int y){
int l=0,r=N;
while(l+1<r){
int mid=l+r>>1;
if(check(x,x+y,mid)&&precheck(x,x+y,mid))l=mid;
else r=mid;
}
return l;
}
void print(int x,int y){
if(!x)return;
int z=tr[x][y];
print(x-1,z);int id=dp[x-1][z]+z-k;
if(z==y){
printf("REPLACE %d ",id+1);cout<<(char)(s2[id+1]-1+'a')<<endl;
}if(z==y+1){
printf("DELETE %d\n",id+1);
}if(z==y-1){
printf("INSERT %d ",id+1);cout<<(char)(s2[id+1]-1+'a')<<endl;
}
}
void DP(){
for(int i=0;i<=k;++i)for(int j=0;j<=(k<<1);++j)dp[i][j]=tr[i][j]=-1;
dp[0][k]=0;int ans=-1;
for(int i=0;i<=k;++i){
for(int j=0;j<=(k<<1);++j){
if(dp[i][j]==-1)continue;
dp[i][j]=dp[i][j]+sol(dp[i][j]+1,j-k);
if((dp[i][j]+1<=n)&&(dp[i+1][j]+1+j-k<=m)&&dp[i+1][j]<dp[i][j]+1){
dp[i+1][j]=dp[i][j]+1;tr[i+1][j]=j;
}
if((j!=(1<<k))&&(dp[i+1][j]+1+j-k<=m)&&dp[i+1][j+1]<dp[i][j]){
dp[i+1][j+1]=dp[i][j];tr[i+1][j+1]=j;
}
if((dp[i][j]+1<=n)&&(j!=0)&&dp[i+1][j-1]<dp[i][j]+1){
dp[i+1][j-1]=dp[i][j]+1;tr[i+1][j-1]=j;
}
}
if(dp[i][k-n+m]==n){ans=i;break;}
}
if(ans==-1){
printf("NO\n");return;
}
printf("YES\n%d\n",ans);
print(ans,k-n+m);
}
void solve(){
scanf("%d%d%d",&n,&m,&k);
for(int i=1;i<=n;++i){char c;cin>>c;s1[i]=c-'a'+1;}
for(int i=1;i<=m;++i){char c;cin>>c;s2[i]=c-'a'+1;}
for(int i=1;i<=n;++i)hash1[i]=hash1[i-1]*base+s1[i];
for(int i=1;i<=m;++i)hash2[i]=hash2[i-1]*base+s2[i];
for(int i=1;i<=n;++i)pre1[i]=pre1[i-1]*base2+s1[i];
for(int i=1;i<=m;++i)pre2[i]=pre2[i-1]*base2+s2[i];
if(k<abs(n-m)){printf("NO\n");return;}
DP();
}
signed main(){
int T;scanf("%d",&T);
pw[0]=pw2[0]=1;for(int i=1;i<N;++i)pw[i]=pw[i-1]*base,pw2[i]=pw2[i-1]*base2;
while(T--)solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 31136kb
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: 3ms
memory: 35764kb
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 REPLACE 1 o REPLACE 2 s REPLACE 3 k REPLACE 5 m REPLACE 6 l REPLACE 8 j REPLACE 9 t REPLACE 10 u REPLACE 11 a REPLA...
result:
ok OK!
Test #3:
score: 0
Accepted
time: 364ms
memory: 44344kb
input:
100 3211 3185 685 wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww...
output:
YES 552 REPLACE 4 i REPLACE 16 b REPLACE 17 s REPLACE 20 u REPLACE 29 j REPLACE 31 b REPLACE 33 z REPLACE 39 l REPLACE 67 l REPLACE 68 s REPLACE 70 i REPLACE 73 p REPLACE 91 g REPLACE 101 x REPLACE 111 y REPLACE 113 x REPLACE 115 s REPLACE 117 f REPLACE 121 m REPLACE 123 a REPLACE 130 h REPLACE 132 ...
result:
ok OK!
Test #4:
score: 0
Accepted
time: 691ms
memory: 74440kb
input:
5 999000 998976 995 llllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll...
output:
YES 894 REPLACE 1682 u REPLACE 2752 y REPLACE 2983 z REPLACE 4433 b REPLACE 7318 m REPLACE 8237 b REPLACE 8898 n REPLACE 9049 m REPLACE 9504 n REPLACE 9903 n REPLACE 13572 q REPLACE 14221 v REPLACE 14615 y REPLACE 16903 j REPLACE 17197 e REPLACE 18073 v REPLACE 19408 b REPLACE 20512 p REPLACE 21033 ...
result:
ok OK!
Test #5:
score: 0
Accepted
time: 742ms
memory: 74308kb
input:
5 999000 998971 1000 lllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll...
output:
NO NO NO NO NO
result:
ok OK!
Test #6:
score: -100
Wrong Answer
time: 428ms
memory: 74348kb
input:
5 1000000 1000000 1000 faafaffaaffafaafaffafaaffaafaffaaffafaaffaafaffafaafaffaaffafaafaffafaaffaafaffafaafaffaaffafaaffaafaffaaffafaafaffafaaffaafaffaaffafaaffaafaffafaafaffaaffafaaffaafaffaaffafaafaffafaaffaafaffafaafaffaaffafaafaffafaaffaafaffaaffafaaffaafaffafaafaffaaffafaafaffafaaffaafaffafaafa...
output:
YES 0 YES 779 DELETE 1 DELETE 1 INSERT 2 l REPLACE 4 j REPLACE 5 d REPLACE 6 v REPLACE 8 t REPLACE 10 i REPLACE 11 b REPLACE 12 w REPLACE 13 o DELETE 15 DELETE 16 REPLACE 16 c REPLACE 18 n REPLACE 19 m INSERT 20 d REPLACE 22 m INSERT 23 x REPLACE 26 x REPLACE 27 w REPLACE 28 n REPLACE 29 y DELETE 31...
result:
wrong answer At test 0 expected 246 got 0