QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#739550 | #5312. Levenshtein Distance | PatrickStar | TL | 0ms | 4604kb | C++14 | 1.6kb | 2024-11-12 22:08:04 | 2024-11-12 22:08:13 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int MAXN = 1e5, base = 31, mod = 1e9+7;
int K, n, m, ans[35];
char a[MAXN+5], b[MAXN+5];
int dp[35][65];
ll H1[MAXN+5], H2[MAXN+5], pw[MAXN+5];
ll get1(int l, int r) {
return (H1[r]-H1[l-1]*pw[r-l+1]%mod+mod)%mod;
}
ll get2(int l, int r) {
return (H2[r]-H2[l-1]*pw[r-l+1]%mod+mod)%mod;
}
int main() {
scanf("%d", &K);
scanf("%s", a+1);
n = strlen(a+1);
scanf("%s", b+1);
m = strlen(b+1);
pw[0] = 1;
for (int p=1;p<=MAXN;p++) {
pw[p] = pw[p-1]*base%mod;
}
for (int p=1;p<=n;p++) {
H1[p] = (H1[p-1]*base+a[p])%mod;
}
for (int p=1;p<=m;p++) {
H2[p] = (H2[p-1]*base+b[p])%mod;
}
for (int p=1;p<=m;p++) {
memset(dp, -0x3f, sizeof(dp));
dp[0][0+K] = 0;
for (int i=0;i<=K;i++) {
for (int j=-K;j<=K;j++) {
if (dp[i][j+K]>=0&&dp[i][j+K]+p-1+j<=m&&dp[i][j+K]+p-1+j>=p-1) {
int p1 = dp[i][j+K], p2 = dp[i][j+K]+p-1+j;
int L = 0, R = min(n-p1, m-p2);
while (L<R) {
int mid = (L+R+1)/2;
if (get1(p1+1, p1+mid)==get2(p2+1, p2+mid)) L = mid;
else R = mid-1;
}
dp[i][j+K]+=L;
dp[i+1][j+K+1] = max(dp[i+1][j+K+1], dp[i][j+K]);
if (j!=-K) dp[i+1][j+K-1] = max(dp[i+1][j+K-1], min(n, dp[i][j+K]+1));
if (dp[i][j+K]!=n) dp[i+1][j+K] = max(dp[i+1][j+K], dp[i][j+K]+1);
}
}
}
for (int j=-K;j<=K;j++) {
for (int i=0;i<=K;i++) {
if (dp[i][j+K]==n&&dp[i][j+K]+p-1+j<=m&&dp[i][j+K]+p-1+j>=p) {
ans[i]++;
break;
}
}
}
}
for (int p=0;p<=K;p++) {
printf("%d\n", ans[p]);
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 4604kb
input:
4 aaa aabbaab
output:
0 5 15 7 1
result:
ok 5 number(s): "0 5 15 7 1"
Test #2:
score: -100
Time Limit Exceeded
input:
30 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA...