QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#390384 | #5312. Levenshtein Distance | 251Sec | WA | 458ms | 15460kb | C++14 | 2.5kb | 2024-04-15 14:40:03 | 2024-04-15 14:40:04 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int f[35][65];
int n, m, k;
char s[100005], t[100005], st[200005];
int sa[200005], rk[200005], rko[200005], sao[200005], bu[200005], h[200005][20];
int ans[35];
void BuildSA() {
int l = n + m + 1, m = 255;
for (int i = 1; i <= l; i++) bu[rk[i] = st[i]]++;
for (int i = 1; i <= m; i++) bu[i] += bu[i - 1];
for (int i = l; i; i--) sa[bu[rk[i]]--] = i;
for (int w = 1; w < l; w <<= 1) {
int p = 0;
for (int i = l; i > l - w; i--) sao[++p] = i;
for (int i = 1; i <= l; i++) if (sa[i] > w) sao[++p] = sa[i] - w;
memset(bu, 0, sizeof(bu));
for (int i = 1; i <= l; i++) bu[rk[i]]++;
for (int i = 1; i <= m; i++) bu[i] += bu[i - 1];
for (int i = l; i; i--) sa[bu[rk[sao[i]]]--] = sao[i];
memcpy(rko, rk, sizeof(rk));
p = 0;
for (int i = 1; i <= l; i++) rk[sa[i]] = (p += rko[sa[i]] != rko[sa[i - 1]] || rko[sa[i] + w] != rko[sa[i - 1] + w]);
m = p;
}
for (int i = 1, k = 0; i <= l; i++) {
if (rk[i] == 1) { k = h[1][0] = 0; continue; }
if (k) k--;
while (st[i + k] == st[sa[rk[i] - 1] + k]) k++;
h[rk[i]][0] = k;
}
for (int j = 1; j < 20; j++) {
for (int i = 1; i + (1 << j) - 1 <= l; i++) {
h[i][j] = min(h[i][j - 1], h[i + (1 << (j - 1))][j - 1]);
}
}
}
int LCP(int a, int b) {
int res = min(n - a + 1, m - b + 1);
if (a > n || b > m) return 0;
a = rk[a], b = rk[b + n + 1];
if (a > b) swap(a, b);
int x = __lg(b - a++);
return min({ h[a][x], h[b - (1 << x) + 1][x], res });
}
int main() {
scanf("%d%s%s", &k, s + 1, t + 1), n = strlen(s + 1), m = strlen(t + 1);
for (int i = 1; i <= n; i++) st[i] = s[i];
st[n + 1] = '$';
for (int i = 1; i <= m; i++) st[i + n + 1] = t[i];
BuildSA();
for (int s = 0; s < m; s++) {
memset(f, 0xc0, sizeof(f));
f[0][k] = 0;
for (int i = 0; i <= k; i++) {
for (int j = -k; j <= k; j++) {
if (f[i][j + k] >= 0) {
f[i][j + k] += LCP(f[i][j + k] + 1, s + f[i][j + k] + j + 1);
if (i < k) {
f[i + 1][j + k] = max(f[i + 1][j + k], f[i][j + k] + 1);
f[i + 1][j + k + 1] = max(f[i + 1][j + k + 1], f[i][j + k]);
f[i + 1][j + k - 1] = max(f[i + 1][j + k - 1], f[i][j + k] + 1);
}
}
}
}
for (int j = min(k, m - n - s), d = 1e9; j >= -min(k, n - 1); j--) {
for (int i = 1; i <= k; i++) {
if (f[i][j + k] == n) {
d = min(d, i);
break;
}
}
if (d <= k) ans[d]++;
d++;
}
}
for (int i = 0; i <= k; i++) printf("%d\n", ans[i]);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 7972kb
input:
4 aaa aabbaab
output:
0 5 15 7 1
result:
ok 5 number(s): "0 5 15 7 1"
Test #2:
score: 0
Accepted
time: 397ms
memory: 15280kb
input:
30 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA...
output:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
result:
ok 31 numbers
Test #3:
score: -100
Wrong Answer
time: 458ms
memory: 15460kb
input:
30 BBAAABAABBBA ABBABBBBAABAAAABABBBAABAABBAABAAAAAABAABABBAAAABAAAAABABAABAABBABBAAAABBBBAABBABAAABAABAABBBBBABBABABBBBABBBBAAABBABABBBBAAABBBBAAAABABABABABABABAAABBBBBAABAABAABBAABBBAABBBBBBBABAABAABAABABAABAAABAABBBAABABBABBAAABAAAABABABBABBBAAABBBAAAABBBBABABABAABAABBBBBBBBBAABBBABABBBABAABBABBA...
output:
0 645 8230 50302 166666 310121 345469 280387 227200 209178 203013 198561 105134 102164 99771 97730 96058 94730 93633 92720 92060 91525 91143 90831 90585 90419 90288 90200 90120 90068 90035
result:
wrong answer 1st numbers differ - expected: '17', found: '0'