QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#401498#850. Edit Distance Yet AgainxuqinAC ✓2320ms47824kbC++144.3kb2024-04-28 20:33:382024-04-28 20:33:39

Judging History

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

  • [2024-04-28 20:33:39]
  • 评测
  • 测评结果:AC
  • 用时:2320ms
  • 内存:47824kb
  • [2024-04-28 20:33:38]
  • 提交

answer

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cassert>
#include<set>
#include<random>
#include<chrono>
#include<bitset>
#include<map>
#include<queue>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<string>
#define eb emplace_back

using namespace std;
const int maxn=1e6+10, maxm=1e6+10, inf=2e9+10;
const double eps=1e-10;
typedef long long LL;
typedef unsigned long long ULL;
const LL INF=4e18;
typedef pair<LL, int> pli;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
inline int read() {
	int x=0, f=1; char c=getchar();
	for(; c<'0'||c>'9'; c=getchar()) if(c=='-') f=0;
	for(; c>='0'&&c<='9'; c=getchar()) x=x*10+c-'0';
	return f?x:-x;
}
int P;
mt19937 rnd((unsigned)chrono::steady_clock::now().time_since_epoch().count());
inline int ksm(int x, int y) {
	int s=1;
	for(; y; y>>=1, x=1LL*x*x%P)
		if(y&1) s=1LL*s*x%P;
	return s;
}
inline int add(int x, int y) {x+=y; return x>=P?x-P:x;}
inline int del(int x, int y) {x-=y; return x<0?x+P:x;}
LL gcd(LL x, LL y) {return y?gcd(y, x%y):x;}

const int B1=1607, B2=1609, M1=1000010747, M2=1000010749;
int pw[3][maxn];
char s[maxn], t[maxn]; int n, m, k;
struct pj{
    int hsh[3][maxn];
    inline void init(int sz, char ss[maxn]) {
        for(int i=1; i<=sz; ++i)
            hsh[1][i]=(1LL*hsh[1][i-1]*B1%M1+ss[i]-'a'+1)%M1,
            hsh[2][i]=(1LL*hsh[2][i-1]*B2%M2+ss[i]-'a'+1)%M2;
    }
    inline pii gethsh(int l, int r) {
        return make_pair((hsh[1][r]-1ll*hsh[1][l-1]*pw[1][r-l+1]%M1+M1)%M1,
                        (hsh[2][r]-1ll*hsh[2][l-1]*pw[2][r-l+1]%M2+M2)%M2);
    }
} S, T;
inline int lcp(int p, int q) {//lcp(s[p, n] t[q, m])
    int l=0, r=min(n-p+1, m-q+1);
    while(l<r) {
        int mid=(l+r+1)>>1;
        if(S.gethsh(p, p+mid-1)==T.gethsh(q, q+mid-1)) l=mid; else r=mid-1;
    }
    return l;
}

int dp[1010][2020], f[2020];
int pr[1010][2020];
inline void sol() {
    n=read(), m=read(), k=read();
    scanf("%s", s+1); S.init(n, s);
    scanf("%s", t+1); T.init(m, t); 
    int M=1010;
    dp[0][M]=lcp(1, 1);//(x, x+i)  (n, n+(m-n))
    if(m==n&&dp[0][M]==n) return printf("YES\n0\n"), void();
    int ii=-1, jj=-1;
    for(int i=1; i<=k; ++i) {
        for(int j=M-i; j<=M+i; ++j) f[j]=-1;
        for(int j=M-(i-1); j<=M+(i-1); ++j) {
            int x=dp[i-1][j];
            if(x==-1) continue;
            int y=x+(j-M);
            //(x, y)->(x+1, y)
            if(x<n) {
                int nx=x+1, ny=y, d=lcp(nx+1, ny+1);
                nx+=d, ny+=d;
                if(nx>f[ny-nx+M]) {
                    f[ny-nx+M]=nx;
                    pr[i][ny-nx+M]=j;
                }
            }
            //(x, y)->(x, y+1)
            if(y<m) {
                int nx=x, ny=y+1, d=lcp(nx+1, ny+1);
                nx+=d, ny+=d;
                if(nx>f[ny-nx+M]) {
                    f[ny-nx+M]=nx;
                    pr[i][ny-nx+M]=j;
                }
            }
            //(x, y)->(x+1, y+1)
            if(x<n&&y<m) {
                int nx=x+1, ny=y+1, d=lcp(nx+1, ny+1);
                nx+=d, ny+=d;
                if(nx>f[ny-nx+M]) {
                    f[ny-nx+M]=nx;
                    pr[i][ny-nx+M]=j;
                }
            }
        }
     //   printf("th=%d\n", i);
        for(int j=M-i; j<=M+i; ++j) {
            dp[i][j]=f[j];
     //       if(f[j]!=-1) printf("(%d, %d)\n", f[j], f[j]+j-M);
        }
        //chk
        if(-i<=m-n&&m-n<=i&&dp[i][M+m-n]==n) {
            ii=i, jj=M+m-n; break;
        }
    }
    if(ii==-1) return puts("NO"), void();
    printf("YES\n%d\n", ii);
    //return;
    while(ii>0) {
        int nw=dp[ii][jj], pre=pr[ii][jj];
     //   printf("pre=%d\n", pre-M);
        int p=dp[ii-1][pre];
        //(nw, nw+jj-M)
        //(p, p+pre-M)
        if(jj==pre) {
            printf("REPLACE %d %c\n", p+1, t[p+pre-M+1]);
        } else if(jj==pre+1) {
            printf("INSERT %d %c\n", p+1, t[p+pre-M+1]);
        } else if(jj==pre-1) {
            printf("DELETE %d\n", p+1);
        }
        --ii; jj=pre;
    }
}

int main() {
    pw[1][0]=pw[2][0]=1;
    for(int i=1; i<=1000000; ++i) 
        pw[1][i]=1LL*pw[1][i-1]*B1%M1, pw[2][i]=1LL*pw[2][i-1]*B2%M2;
    int T=read();
    while(T--) sol();
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 8ms
memory: 18968kb

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: 11ms
memory: 20124kb

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: 1467ms
memory: 29604kb

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: 1895ms
memory: 46524kb

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: 2320ms
memory: 47256kb

input:

5
999000 998971 1000
lllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll...

output:

NO
NO
NO
NO
NO

result:

ok OK!

Test #6:

score: 0
Accepted
time: 489ms
memory: 47824kb

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: 1771ms
memory: 47436kb

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!