QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#390751 | #5312. Levenshtein Distance | jiajia | WA | 1ms | 3920kb | C++20 | 2.8kb | 2024-04-15 21:07:42 | 2024-04-15 21:07:43 |
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 = __builtin_clz(b - a++) ^ 31;
return min({ h[a][x], h[b - (1 << x) + 1][x]});
}
int main() {
freopen("in.txt", "r", stdin);
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++) {
// cout << "s: " << s << '\n';
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);
}
}
}
}
// cerr << f[2][2] << '\n';
for (int j = min(k, m - n - s), d = 1e9; j >= -min(k, n - 1); j--) {
bool tag = 0;
for (int i = 0; i <= k; i++) {
if (f[i][j + k] >= n) {
// cerr << d << ' ' << i << ' ' << j << '\n';
// d = min(d, i);
// tag = 1;
ans[i]++;
break;
}
}
// if (d <= k && !tag) cerr << d << ' ' << j << '\n';
// 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: 0
Wrong Answer
time: 1ms
memory: 3920kb
input:
4 aaa aabbaab
output:
0
result:
wrong answer Answer contains longer sequence [length = 5], but output contains 1 elements