QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#21308 | #1276. String Distance | FudanU1# | AC ✓ | 340ms | 6424kb | C++17 | 1.8kb | 2022-03-04 14:53:59 | 2022-05-08 02:51:56 |
Judging History
answer
#include <bits/stdc++.h>
#define rep(i, l, r) for (int i = l; i <= r; i++)
#define per(i, r, l) for (int i = r; i >= l; i--)
#define srep(i, l, r) for (int i = l; i < r; i++)
#define sper(i, r, l) for (int i = r; i > l; i--)
#define maxn 200022
#define maxm 32
#define maxsig 26
using namespace std;
int n, m, q;
int dp[maxm][maxm];
char s[maxn], t[maxm];
int fir[maxm][maxsig];
struct query{
int x, y, i;
query(int x, int y, int i) : x(x), y(y), i(i){}
query(){}
bool operator < (const query b) const{
return x == b.x ? y == b.y ? i < b.i : y < b.y : x < b.x;
}
}que[maxn];
int ans[maxn];
int main(){
int T, x, y; scanf("%d", &T);
while (T--) {
scanf("%s%s", s + 1, t + 1);
n = strlen(s + 1), m = strlen(t + 1);
rep(k, 1, m) {
rep(i, 0, 25) {
fir[k][i] = m + 22;
rep(j, k, m) {
if (t[j] - 'a' == i) {
fir[k][i] = j;
break;
}
}
}
}
scanf("%d", &q);
rep(i, 1, q) {
scanf("%d%d", &x, &y);
que[i] = query(x, y, i);
}
sort(que + 1, que + 1 + q);
int ptr = q;
rep(p, 1, m + 1) {
rep(l, 1, m) {
dp[p][l] = n + 22;
}
}
per(i, n, 1) {
rep(p, 1, m) {
per(l, m, 1) {
if (fir[p][s[i] - 'a'] <= m) {
int dd = -1;
if (l - 1 == 0) dd = i;
else dd = dp[fir[p][s[i] - 'a'] + 1][l - 1];
dp[p][l] = min(dp[p][l], dd);
}
}
}
while (ptr >= 1 && que[ptr].x == i) {
per(l, m, 1) {
if (que[ptr].y >= dp[1][l]) {
ans[que[ptr].i] = (que[ptr].y - que[ptr].x + 1) + m - 2 * l;
goto done;
}
}
ans[que[ptr].i] = (que[ptr].y - que[ptr].x + 1) + m;
done:ptr--;
}
}
rep(i, 1, q) printf("%d\n", ans[i]);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 5856kb
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: 340ms
memory: 6424kb
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