#include <bits/stdc++.h> using namespace std; #define JOIN_(X, Y) X##Y #define JOIN(X, Y) JOIN_(X, Y) #define TMP JOIN(tmp, __LINE__) #define PB push_back #define SZ(x) int((x).size()) #define REP(i, n) for (int i = 0, TMP = (n); i < TMP; ++i) #define FOR(i, a, b) for (int i = (a), TMP = (b); i <= TMP; ++i) #define FORD(i, a, b) for (int i = (a), TMP = (b); i >= TMP; --i) #ifdef DEBUG #define DEB(x) (cerr << x) #else #define DEB(x) #endif typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pii; const int INF = 1e9 + 9; struct Query { int id; vector<int> points; Query() : points(4) {} void read(int _id) { id = _id; REP(i, 4) { cin >> points[i]; } } int len1() { return points[1] - points[0] + 1; } int len2() { return points[3] - points[2] + 1; } bool operator<(const Query &other) const { return points < other.points; } }; int lcs(char *a, char *b, int n, int m) { vector<vector<int>> dp(n + 1, vector<int>(m + 1, 0)); FOR(i, 1, n) { FOR(j, 1, m) { if (a[i] == b[j]) { dp[i][j] = dp[i - 1][j - 1] + 1; } else { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } } return dp[n][m]; } void inline one() { int n, m, q; cin >> n >> m >> q; char a[3003], b[3003]; cin >> (a + 1) >> (b + 1); vector<Query> queries; REP(query_id, q) { Query query; query.read(query_id); queries.PB(query); } sort(queries.begin(), queries.end()); vi results(q, -1); for (auto &query : queries) { int result = lcs(a + query.points[0] - 1, b + query.points[2] - 1, query.len1(), query.len2()); DEB("query.id=" << query.id << " result=" << result << "\n"); results[query.id] = result; } for (int result : results) { cout << result << "\n"; } } int main() { ios::sync_with_stdio(false); cin.tie(0); // int z; cin >> z; while(z--) one(); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | #include <bits/stdc++.h> using namespace std; #define JOIN_(X, Y) X##Y #define JOIN(X, Y) JOIN_(X, Y) #define TMP JOIN(tmp, __LINE__) #define PB push_back #define SZ(x) int((x).size()) #define REP(i, n) for (int i = 0, TMP = (n); i < TMP; ++i) #define FOR(i, a, b) for (int i = (a), TMP = (b); i <= TMP; ++i) #define FORD(i, a, b) for (int i = (a), TMP = (b); i >= TMP; --i) #ifdef DEBUG #define DEB(x) (cerr << x) #else #define DEB(x) #endif typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pii; const int INF = 1e9 + 9; struct Query { int id; vector<int> points; Query() : points(4) {} void read(int _id) { id = _id; REP(i, 4) { cin >> points[i]; } } int len1() { return points[1] - points[0] + 1; } int len2() { return points[3] - points[2] + 1; } bool operator<(const Query &other) const { return points < other.points; } }; int lcs(char *a, char *b, int n, int m) { vector<vector<int>> dp(n + 1, vector<int>(m + 1, 0)); FOR(i, 1, n) { FOR(j, 1, m) { if (a[i] == b[j]) { dp[i][j] = dp[i - 1][j - 1] + 1; } else { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } } return dp[n][m]; } void inline one() { int n, m, q; cin >> n >> m >> q; char a[3003], b[3003]; cin >> (a + 1) >> (b + 1); vector<Query> queries; REP(query_id, q) { Query query; query.read(query_id); queries.PB(query); } sort(queries.begin(), queries.end()); vi results(q, -1); for (auto &query : queries) { int result = lcs(a + query.points[0] - 1, b + query.points[2] - 1, query.len1(), query.len2()); DEB("query.id=" << query.id << " result=" << result << "\n"); results[query.id] = result; } for (int result : results) { cout << result << "\n"; } } int main() { ios::sync_with_stdio(false); cin.tie(0); // int z; cin >> z; while(z--) one(); } |