QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#55025#1276. String DistanceYaoBIG#AC ✓1651ms16960kbC++1.9kb2022-10-11 22:56:152022-10-11 22:56:18

Judging History

This is the latest submission verdict.

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-10-11 22:56:18]
  • Judged
  • Verdict: AC
  • Time: 1651ms
  • Memory: 16960kb
  • [2022-10-11 22:56:15]
  • Submitted

answer

#include "bits/stdc++.h"
#define rep(i, a, n) for (auto i = a; i <= (n); i++)
#define revrep(i, a, n) for (auto i = n; i >= (a); i--)
#define all(a) a.begin(), a.end()
#define sz(a) (int)(a).size()
template<class T> bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } else return 0; }
template<class T> bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } else return 0; }
using namespace std;

template<class A, class B> string to_string(pair<A, B> p);
template<class A> string to_string(A v) {
	bool first = 1;
	string res = "{";
	for (auto x: v) {
		if (!first) res += ", ";
		first = 0;
		res += to_string(x);
	}
	res += "}";
	return res;
}
template<class A, class B> string to_string(pair<A, B> p) {
	return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

void debug_out() { cerr << endl; }
template<class H, class... T> void debug_out(H h, T... t) {
	cerr << " " << to_string(h);
	debug_out(t...);
}

#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)

using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;

const int inf = 0x3f3f3f3f;

int main() {
	ios::sync_with_stdio(0); cin.tie(0);

	int cas; cin >> cas; while (cas--) {
		string s, t; cin >> s >> t;
		int n = sz(s), m = sz(t);
		int q; cin >> q;
		vvi nxt(n + 1, vi(26, inf));
		revrep(i, 0, n - 1) {
			int c = s[i] - 'a';
			nxt[i] = nxt[i + 1];
			nxt[i][c] = i;
		}
		while (q--) {
			int l, r; cin >> l >> r;
			l--, r--;
			vi dp(m + 1, inf);
			dp[0] = l - 1;
			rep(ind, 0, m - 1) {
				int c = t[ind] - 'a';
				vi ndp = dp;
				rep(i, 0, m - 1) {
					if (dp[i] + 1 <= r && nxt[dp[i] + 1][c] <= r) chmin(ndp[i + 1], nxt[dp[i] + 1][c]);
				}
				swap(dp, ndp);
			}
			int lcs = 0;
			rep(i, 0, m) if (dp[i] <= r) chmax(lcs, i);
			printf("%d\n", r - l + 1 + m - lcs * 2);
		}		
	}
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 3ms
memory: 3744kb

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: 1651ms
memory: 16960kb

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