QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#810745 | #1276. String Distance | rlc202204 | AC ✓ | 857ms | 14200kb | C++17 | 1.2kb | 2024-12-12 10:27:29 | 2024-12-12 10:27:29 |
Judging History
answer
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1e5 + 5;
const int M = 22;
const int S = 26;
const int inf = 0x3f3f3f3f;
int n, m;
char a[N], b[M];
int nxt[N][S] = {{0}};
int f[M][M] = {{0}};
void cmn(int &x, int y) {
x = min(x, y);
}
void slv() {
scanf("%s", a + 1);
scanf("%s", b + 1);
n = strlen(a + 1), m = strlen(b + 1);
for (int j = 0; j < S; j++)
nxt[n][j] = n + 1;
for (int i = n - 1; i >= 0; i--) {
for (int j = 0; j < S; j++)
nxt[i][j] = nxt[i + 1][j];
nxt[i][a[i + 1] - 'a'] = i + 1;
}
int q;
scanf("%d", &q);
while (q--) {
int l, r;
scanf("%d%d", &l, &r);
for (int i = 0; i <= m; i++)
for (int j = 0; j <= m; j++)
f[i][j] = inf;
f[0][0] = l - 1;
for (int i = 0; i <= m; i++) {
for (int j = 0; j <= m; j++) {
if (i > 0)
cmn(f[i][j], f[i - 1][j]);
if (i > 0 && j > 0 && f[i - 1][j - 1] != inf && nxt[f[i - 1][j - 1]][b[i] - 'a'] <= r)
cmn(f[i][j], nxt[f[i - 1][j - 1]][b[i] - 'a']);
}
}
int ans = 0;
for (int i = m; i >= 1; i--)
if (f[m][i] <= r) {
ans = i;
break;
}
printf("%d\n", r - l + 1 + m - 2 * ans);
}
}
int main() {
int T;
scanf("%d", &T);
while (T--)
slv();
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3912kb
input:
1 qaqaqwqaqaq qaqwqaq 3 1 7 2 8 3 9
output:
4 2 0
result:
ok 3 lines
Test #2:
score: 0
Accepted
time: 857ms
memory: 14200kb
input:
10 rtedxofnuatdpogdzftepqhlwiavwisshpvlcwdkzlccofacdisafkpzmppgdwahposjlgqxqdupksokprgaymznbtyuenyikazrmjonfqawzcmuaqowrizdortxplnogmntadqqpwgolfcyqswtncqldgkrmgbovkyaxsuqwzftujheouhiynkjbzdnnhmreheoawkkljxmiwghewmqvjvmywukckjzwvfnjomthjkvijujdksawjmfrrgmhvpqazesdmeyjvougzmhlxrqmdyripgomcrawinxmfiyr...
output:
21 22 23 24 25 26 27 28 29 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 61 62 63 64 65 64 65 66 67 68 69 70 71 72 73 74 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 92 93 94 95 96 95 96 97 98 99 100 101 102 103 102 1...
result:
ok 995160 lines