QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#390328 | #5312. Levenshtein Distance | suomynonA | RE | 499ms | 17172kb | C++20 | 3.0kb | 2024-04-15 11:53:09 | 2024-04-15 11:53:09 |
Judging History
answer
#include <bits/stdc++.h>
namespace Suffix_Array {
const int N = 2e5, B = 20;
std::string s;
int n, rk[N * 2 + 5], sa[N + 5];
int m, id[N * 2 + 5], cnt[N + 5];
int h[N + 5], st[N + 5][B + 5];
void build(std::string _s) {
s = _s, n = s.length() - 1, m = 2e2;
for (int i = 1; i <= n; ++i) ++cnt[rk[i] = s[i]];
for (int i = 1; i <= m; ++i) cnt[i] += cnt[i - 1];
for (int i = n; i; --i) sa[cnt[rk[i]]--] = i;
for (int i = 1; i <= m; ++i) cnt[i] = 0;
for (int k = 1; k <= n; k <<= 1) {
int num = 0;
for (int i = n - k + 1; i <= n; ++i) id[++num] = i;
for (int i = 1; i <= n; ++i) if (sa[i] > k) id[++num] = sa[i] - k;
for (int i = 1; i <= n; ++i) ++cnt[rk[i]];
for (int i = 1; i <= m; ++i) cnt[i] += cnt[i - 1];
for (int i = n; i; --i) sa[cnt[rk[id[i]]]--] = id[i];
for (int i = 1; i <= m; ++i) cnt[i] = 0;
for (int i = 1; i <= n; ++i) std::swap(id[i], rk[i]);
m = 0;
for (int i = 1; i <= n; ++i) {
if (id[sa[i]] == id[sa[i - 1]] and id[sa[i] + k] == id[sa[i - 1] + k]) rk[sa[i]] = m;
else rk[sa[i]] = ++m;
}
if (n == m) break;
}
for (int i = 1; i <= n; ++i) {
h[i] = std::max(h[i - 1] - 1, 0);
while (s[i + h[i]] == s[sa[rk[i] - 1] + h[i]]) ++h[i];
}
for (int i = 1; i <= n; ++i) st[i][0] = h[sa[i]];
for (int j = 1; (1 << j) <= n; ++j)
for (int i = 1; i + (1 << j) - 1 <= n; ++i) st[i][j] = std::min(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
}
int lcp(int x, int y) {
x = rk[x], y = rk[y];
if (x > y) std::swap(x, y);
int k = std::__lg(y - x);
return std::min(st[x + 1][k], st[y - (1 << k) + 1][k]);
}
}
const int K = 30, Inf = 1e9;
int k, m, n, f[K + 5][K + 5];
std::string s, t;
int lcp(int x, int y) {
if (x >= m or y >= n) return 0;
return Suffix_Array::lcp(x + 1, y + 2 + m);
}
int ans[K + 5];
int main() {
std::ios::sync_with_stdio(false), std::cin.tie(0), std::cout.tie(0);
std::cin >> k >> s >> t, m = s.length(), n = t.length(), Suffix_Array::build((char)0 + s + (char)'|' + t);
for (int x = n - 1; ~x; --x) {
for (int i = 0; i <= k; ++i) for (int j = -k; j <= k; ++j) f[i][j + k] = -Inf;
f[0][k] = 0;
for (int i = 0; i <= k; ++i) for (int j = -k; j <= k; ++j) if (f[i][j + k] > -Inf) {
f[i][j + k] += lcp(f[i][j + k], x + f[i][j + k] + j);
if (i < k) {
f[i + 1][j - 1 + k] = std::max(f[i + 1][j - 1 + k], f[i][j + k] + 1);
f[i + 1][j + 1 + k] = std::max(f[i + 1][j + 1 + k], f[i][j + k]);
f[i + 1][j + k] = std::max(f[i + 1][j + k], f[i][j + k] + 1);
}
}
for (int j = std::min(k, n - x - m), dis = Inf; j >= -std::min(k, m - 1); --j) {
for (int i = 0; i <= k; ++i) if (f[i][j + k] == m) { dis = std::min(dis, i); break; }
if (dis <= k) ++ans[dis], ++dis;
}
}
for (int i = 0; i <= k; ++i) std::cout << ans[i] << "\n";
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 9964kb
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: 499ms
memory: 17172kb
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
Runtime Error
input:
30 BBAAABAABBBA ABBABBBBAABAAAABABBBAABAABBAABAAAAAABAABABBAAAABAAAAABABAABAABBABBAAAABBBBAABBABAAABAABAABBBBBABBABABBBBABBBBAAABBABABBBBAAABBBBAAAABABABABABABABAAABBBBBAABAABAABBAABBBAABBBBBBBABAABAABAABABAABAAABAABBBAABABBABBAAABAAAABABABBABBBAAABBBAAAABBBBABABABAABAABBBBBBBBBAABBBABABBBABAABBABBA...