QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#390377 | #5312. Levenshtein Distance | 251Sec | RE | 0ms | 0kb | C++14 | 2.5kb | 2024-04-15 14:28:11 | 2024-04-15 14:28:12 |
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);
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: 0
Runtime Error
input:
4 aaa aabbaab