QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#390382 | #5312. Levenshtein Distance | zhaohaikun | TL | 3339ms | 23808kb | C++20 | 3.7kb | 2024-04-15 14:37:50 | 2024-04-15 14:37:50 |
Judging History
answer
// MagicDark
#include <bits/stdc++.h>
#define debug cerr << "\033[33m[" << __LINE__ << "]\033[0m "
#define SZ(x) (int) x.size() - 1
#define all(x) x.begin(), x.end()
#define ms(x, y) memset(x, y, sizeof x)
#define F(i, x, y) for (int i = (x); i <= (y); i++)
#define DF(i, x, y) for (int i = (x); i >= (y); i--)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
template <typename T> T& chkmax(T &x, T y) {return x = max(x, y);}
template <typename T> T& chkmin(T &x, T y) {return x = min(x, y);}
template <typename T> T& read(T &x) {
x = 0; int f = 1; char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
return x *= f;
}
const int N = 2e5 + 10;
int lg(int x) {return 31 ^ __builtin_clz(x);}
int n, m, k;
string s, t;
namespace SA {
int n, cnt[N], rk[N], w[N], sa[N], m = 256, height[N], minn[21][N];
void qsort() {
F(i, 1, m) cnt[i] = 0;
F(i, 1, n) cnt[rk[i]]++;
F(i, 1, m) cnt[i] += cnt[i - 1];
DF(i, n, 1) sa[cnt[rk[w[i]]]--] = w[i];
}
void SA(string st) {
n = st.size();
st = ' ' + st;
F(i, 1, n) rk[i] = st[i], w[i] = i;
qsort();
// F(i, 1, n) cout << sa[i] << " "; cout << endl;
for (int len = 1; len <= n; len <<= 1) {
int tot = 0;
F(i, n - len + 1, n) w[++tot] = i;
F(i, 1, n)
if (sa[i] > len) w[++tot] = sa[i] - len;
qsort();
swap(w, rk);
m = 0;
F(i, 1, n) {
if (w[sa[i]] != w[sa[i - 1]] || w[sa[i] + len] != w[sa[i - 1] + len]) m++;
rk[sa[i]] = m;
}
if (m == n) break;
}
int lcp = 0;
st += '#';
// debug << st << endl;
F(i, 1, n) {
if (rk[i] == 1) continue;
if (lcp) lcp--;
while (st[i + lcp] == st[sa[rk[i] - 1] + lcp]) lcp++;
height[rk[i]] = lcp;
}
F(i, 0, lg(n))
F(j, 1, n - (1 << i) + 1)
if (i) minn[i][j] = min(minn[i - 1][j], minn[i - 1][j + (1 << (i - 1))]);
else minn[i][j] = height[j];
}
int lcp(int x, int y) {
x = rk[x], y = rk[y];
if (x > y) swap(x, y);
int k = lg(y - x++);
return min(minn[k][x], minn[k][y - (1 << k) + 1]);
}
}
int qq(int x, int y) {
if (x > n || y > m) return 0;
return min(n - x + 1, SA::lcp(x, n + y));
}
int dp[35][65], ans[35];
signed main() {
cin.tie(0) -> sync_with_stdio(0); // don't use puts
cin >> k >> t >> s;
n = s.size(), m = t.size();
SA::SA(s + t);
// debug << SA::lcp(1, 2) << endl;
// return 0;
// while (true) {
// int x, y; cin >> x >> y;
// cout << qq(x, y) << '\n';
// }
F(be, 1, n) {
ms(dp, -0x3f);
dp[0][30] = qq(be, 1);
F(i, 0, k - 1)
F(j, - i, i) if (dp[i][j + 30] >= 0) {
// if (be + j + dp[i][j + 30] > n) continue;
// if (1 + dp[i][j + 30] > m) continue;
auto work = [&] (int val) {
if (val < -j) return;
if (be + j + val <= n && 1 + val <= m) chkmax(dp[i + 1][j + 30], val + 1 + qq(be + j + 1 + val, 1 + 1 + val));
if (be + j + val <= n) chkmax(dp[i + 1][j + 1 + 30], val + qq(be + j + 1 + val, 1 + val));
if (1 + val <= m) chkmax(dp[i + 1][j - 1 + 30], val + 1 + qq(be + j + val, 1 + 1 + val));
};
work(dp[i][j + 30]);
if (dp[i][j + 30]) work(dp[i][j + 30] - 1);
if (dp[i][j + 30] >= 2) work(dp[i][j + 30] - 2);
}
// debug << dp[2][- 2 + 30] << endl;
F(j, -k, k)
F(i, 0, k)
if (1 + dp[i][j + 30] > m) {
int l = be, r = be + j + dp[i][j + 30] - 1;
// debug << l << " " << r << " " << s.substr(l - 1, r - l + 1) << " " << i << endl;
// debug << be << " " << i << " " << j << " " << dp[i][j + 30] << endl;
if (l <= r ) ans[i]++;
break;
}
// if (be == 1) {
// debug << dp[0][30] << endl;
// }
}
F(i, 0, k) cout << ans[i] << '\n';
return 0;
}
/* why?
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 12576kb
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: 2377ms
memory: 20848kb
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: 0
Accepted
time: 2252ms
memory: 23440kb
input:
30 BBAAABAABBBA ABBABBBBAABAAAABABBBAABAABBAABAAAAAABAABABBAAAABAAAAABABAABAABBABBAAAABBBBAABBABAAABAABAABBBBBABBABABBBBABBBBAAABBABABBBBAAABBBBAAAABABABABABABABAAABBBBBAABAABAABBAABBBAABBBBBBBABAABAABAABABAABAAABAABBBAABABBABBAAABAAAABABABBABBBAAABBBAAAABBBBABABABAABAABBBBBBBBBAABBBABABBBABAABBABBA...
output:
17 662 8230 50302 166666 310121 345469 280387 227200 209178 203013 198561 105117 102147 99771 97730 96058 94730 93633 92720 92060 91525 91143 90831 90585 90419 90288 90200 90120 90068 90035
result:
ok 31 numbers
Test #4:
score: 0
Accepted
time: 3339ms
memory: 21348kb
input:
30 AAABBAAAAAAAAAAAAABAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAABAAAAABAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABABAAAABAAAABAAAABAAAAABAAAAAABAAAAAAAAABAAABAAABBAAAAAAAAAAAAAAAABAABAAABAAAAAAAAAAAAABAAAAAAAAAAAAAAAABAAABBAAAAAAAAAAAAAAAAAAAAAAAAAABAABAABABAAAAAAAAAAAAAAA...
output:
0 0 0 0 1 28 263 1410 6434 22523 56017 115080 189633 263874 316906 339254 332825 312943 286470 263283 246310 235032 227182 221294 216978 213734 211178 208848 206945 205393 204279
result:
ok 31 numbers
Test #5:
score: 0
Accepted
time: 2349ms
memory: 23476kb
input:
30 ABABAABAAABAAAA AABBABABBBBBAAABABAABBAAABBBAABABBBABABABABABBABBBAAAAABBAAABBABABABABABBAAABBAAABAAABBBBBBBAABABBBAAAAABAABBBABBABBBABBBABABAABABBBAAABBABBAAABBBBBBBAABAABABAAAAAABBAABAAABBBAABBAABBAABBABABAAAAAAAABBBBBAAABABABBABAAAAABAABAABAABAABAAABABBABBBABAAABBBABABAABAABBBAABABBAABBAAAABAB...
output:
2 125 1938 15793 70756 178090 282937 325020 310617 277899 244094 217672 206966 202456 198644 105535 102767 100355 98411 96655 95310 94124 93217 92487 91872 91414 91039 90741 90555 90397 90276
result:
ok 31 numbers
Test #6:
score: 0
Accepted
time: 3156ms
memory: 20908kb
input:
30 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA...
output:
0 0 0 0 1 91 1082 14598 58877 123681 212928 258264 253856 277926 265254 243456 217809 198008 190004 184665 184470 183593 183524 183406 183335 183255 183185 183142 183078 182991 182922
result:
ok 31 numbers
Test #7:
score: 0
Accepted
time: 2704ms
memory: 21484kb
input:
30 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAAAAABABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA...
output:
1 460 44032 126686 201431 222795 213509 199190 187254 184699 184159 183907 183818 183558 183500 183466 183412 183393 183343 183299 183275 183236 183163 183127 183105 183056 183010 182971 182910 182719 182437
result:
ok 31 numbers
Test #8:
score: 0
Accepted
time: 2665ms
memory: 23808kb
input:
30 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA...
output:
29 25121 100687 175758 199099 210112 187931 180903 178707 178497 178494 178497 178495 178496 178492 178492 178492 178491 178490 178491 178490 178490 178492 178489 178489 178488 178487 178487 178487 178488 178487
result:
ok 31 numbers
Test #9:
score: 0
Accepted
time: 2682ms
memory: 20980kb
input:
30 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA...
output:
17 12879 77552 170649 187824 198210 200962 189247 181730 180843 178608 178607 178607 178605 178599 178599 178599 178596 178595 178592 178594 178595 178593 178594 178593 178595 178593 178593 178594 178593 178591
result:
ok 31 numbers
Test #10:
score: 0
Accepted
time: 2967ms
memory: 21184kb
input:
30 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA...
output:
0 0 0 0 73 320 52858 125690 177498 270124 217932 192611 177148 228465 180709 166240 162360 162360 162359 162359 162359 162359 162358 162358 162359 162357 162357 162357 162357 162358 162358
result:
ok 31 numbers
Test #11:
score: 0
Accepted
time: 2900ms
memory: 21460kb
input:
30 ABBABABABAAABAAAAAAABAAABBAAAA BABBABBBAAABBAAAAABBBBBAAAABAAAAAABBAABAAAABAAABAAAAAAAABAAABAABAABAABBBAAAABAAAABABBABBAAABABAAAAAAAABAABBAAAAAABABAAAAABABAAAAAAABAAAAAAAABAAABBBAAAAABAABAABAAAAABAAAAAAAAAAAABABAAAABABAAAABAABBAAAAABBAAABABBBBBBAABABAAAAABABBBAABABBAAAABAAAAABABABBAAAAAAAABABBBAB...
output:
0 0 1 38 617 4567 22647 76780 180046 296249 362349 361655 325883 289074 263906 246552 233312 221488 212319 205320 200921 198057 196229 194923 193749 192649 191581 190728 189773 188925 98116
result:
ok 31 numbers
Test #12:
score: 0
Accepted
time: 1984ms
memory: 21192kb
input:
30 Y3YYY wwwwwwwYYwwwww3ww3w33Yww3wYY3w3w33w3Y3YY33wYwY33wwYwY33Y3w3wwwY333ww333Ywww3w3Y33wwYw3YYwY3Y3w33YwwwwwYYYw3YYYYYYww3w3333Yw3Y3wY3wYw3w3w3Y3w33YY33YwwY3Y3wY33w3333Yw333wYYw3Yw33w3YwYYYYYwwwwY3wwYYYYY33Y3YwYY3Yw3www3Ywww333w3YYY333YY3Y3333Y3Y33ww33333wwYY3YYYww3YYYYYYwwYYYwwYY33Y33wYY3w3wY3w3...
output:
412 9896 68598 212288 338054 201716 131998 125202 120710 117075 114099 111708 109603 107820 106320 105086 104074 103198 102592 102094 101640 101290 101006 100767 100571 100430 100305 100226 100158 100109 100074
result:
ok 31 numbers
Test #13:
score: -100
Time Limit Exceeded
input:
30 BnAnnBB3fn33BffBBn3nfAABnBnfBfffBfBAf3Ann3nnfAnAAA 3Bn33ABnBnn3ffnfnnn3fB3Af33fBf3fnnBnBAAffBB33BnB3A3nBAnnABnf33AAAB3nnBA33nf33n3nBfnfnfABB3nBAA3ABAnAAB33A33ABfnAnA33f3fBBnBBfBnB3f3BBf3AAfBAAB3A3BnffBnf33nnffA33fAnffnBBB3nnBBfnBB33BffABn3n3BnAffBBA3nnnAfBfBnn3nn3fn3fn3B3AB3fBn3ABf3BAfAnAB3AAABB3...