#include <bits/stdc++.h> using namespace std; struct LCS { int m; string t; vector <vector<int>> f, g; LCS(const string &t): m(t.size()), t(t) { f = g = {vector <int> (m + 1, 0)}; iota(f[0].begin(), f[0].end(), 0); } void addChar(char c) { int i = f.size(); f.emplace_back(m + 1, 0); g.emplace_back(m + 1, 0); for (int j = 1; j <= m; j++) { if (c == t[j - 1]) { f[i][j] = g[i][j - 1]; g[i][j] = f[i - 1][j]; } else { f[i][j] = max(f[i - 1][j], g[i][j - 1]); g[i][j] = min(g[i][j - 1], f[i - 1][j]); } } } vector <int> query(int pref, int l, int r) { if (l > r) { return {0}; } int len = r - l + 1; vector <int> ans(len + 1, 0); int ansCurr = 0; for (int i = 1; i <= len; i++) { ans[i] += ans[i - 1]; int k = r + 1 - i; if (k >= f[pref][k + 1]) { ans[i]++; int pos = f[pref][k + 1] - 1; if (pos >= l) { ans[r - pos + 1]--; } } } return ans; } }; struct query { int sl, sr, tl, tr; }; void divideAndConquer(int l, int r, const string &s, const string &t, const string &tRev, const vector <int> &queryIds, const vector <query> &queries, vector <int> &ans) { if (l > r || queryIds.empty()) { return ; } int mid = (l + r) / 2; vector <int> queryIdsLeft, queryIdsRight, queryIdsHere; for (int i : queryIds) { if (queries[i].sr < mid) { queryIdsLeft.push_back(i); } else if (queries[i].sl > mid + 1) { queryIdsRight.push_back(i); } else { queryIdsHere.push_back(i); } } divideAndConquer(l, mid, s, t, tRev, queryIdsLeft, queries, ans); divideAndConquer(mid + 1, r, s, t, tRev, queryIdsRight, queries, ans); LCS left(tRev), right(t); for (int i = mid; i >= l; i--) { left.addChar(s[i]); } for (int i = mid + 1; i <= r; i++) { right.addChar(s[i]); } for (int i : queryIdsHere) { int leftPref = mid + 1 - queries[i].sl; int rightPref = queries[i].sr - mid; auto resLeft = left.query(leftPref, t.size() - 1 - queries[i].tr, t.size() - 1 - queries[i].tl); auto resRight = right.query(rightPref, queries[i].tl, queries[i].tr); for (int j = queries[i].tl - 1; j <= queries[i].tr; j++) { ans[i] = max(ans[i], resLeft[j - queries[i].tl + 1] + resRight[queries[i].tr - j]); } } } vector <int> solve(int n, int m, int q, const string &s, const string &t, const vector <query> &queries) { auto tRev = t; reverse(tRev.begin(), tRev.end()); vector <int> ans(q, -1); vector <int> queryIds(q); iota(queryIds.begin(), queryIds.end(), 0); divideAndConquer(0, n - 1, s, t, tRev, queryIds, queries, ans); return ans; } int main() { ios_base::sync_with_stdio(false); int n, m, q; cin >> n >> m >> q; string s, t; cin >> s >> t; vector <query> queries; for (int i = 0; i < q; i++) { int sl, sr, tl, tr; cin >> sl >> sr >> tl >> tr; sl--; sr--; tl--; tr--; queries.push_back({sl, sr, tl, tr}); } auto ans = solve(n, m, q, s, t, queries); for (int x : ans) { cout << x << '\n'; } return 0; }
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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | #include <bits/stdc++.h> using namespace std; struct LCS { int m; string t; vector <vector<int>> f, g; LCS(const string &t): m(t.size()), t(t) { f = g = {vector <int> (m + 1, 0)}; iota(f[0].begin(), f[0].end(), 0); } void addChar(char c) { int i = f.size(); f.emplace_back(m + 1, 0); g.emplace_back(m + 1, 0); for (int j = 1; j <= m; j++) { if (c == t[j - 1]) { f[i][j] = g[i][j - 1]; g[i][j] = f[i - 1][j]; } else { f[i][j] = max(f[i - 1][j], g[i][j - 1]); g[i][j] = min(g[i][j - 1], f[i - 1][j]); } } } vector <int> query(int pref, int l, int r) { if (l > r) { return {0}; } int len = r - l + 1; vector <int> ans(len + 1, 0); int ansCurr = 0; for (int i = 1; i <= len; i++) { ans[i] += ans[i - 1]; int k = r + 1 - i; if (k >= f[pref][k + 1]) { ans[i]++; int pos = f[pref][k + 1] - 1; if (pos >= l) { ans[r - pos + 1]--; } } } return ans; } }; struct query { int sl, sr, tl, tr; }; void divideAndConquer(int l, int r, const string &s, const string &t, const string &tRev, const vector <int> &queryIds, const vector <query> &queries, vector <int> &ans) { if (l > r || queryIds.empty()) { return ; } int mid = (l + r) / 2; vector <int> queryIdsLeft, queryIdsRight, queryIdsHere; for (int i : queryIds) { if (queries[i].sr < mid) { queryIdsLeft.push_back(i); } else if (queries[i].sl > mid + 1) { queryIdsRight.push_back(i); } else { queryIdsHere.push_back(i); } } divideAndConquer(l, mid, s, t, tRev, queryIdsLeft, queries, ans); divideAndConquer(mid + 1, r, s, t, tRev, queryIdsRight, queries, ans); LCS left(tRev), right(t); for (int i = mid; i >= l; i--) { left.addChar(s[i]); } for (int i = mid + 1; i <= r; i++) { right.addChar(s[i]); } for (int i : queryIdsHere) { int leftPref = mid + 1 - queries[i].sl; int rightPref = queries[i].sr - mid; auto resLeft = left.query(leftPref, t.size() - 1 - queries[i].tr, t.size() - 1 - queries[i].tl); auto resRight = right.query(rightPref, queries[i].tl, queries[i].tr); for (int j = queries[i].tl - 1; j <= queries[i].tr; j++) { ans[i] = max(ans[i], resLeft[j - queries[i].tl + 1] + resRight[queries[i].tr - j]); } } } vector <int> solve(int n, int m, int q, const string &s, const string &t, const vector <query> &queries) { auto tRev = t; reverse(tRev.begin(), tRev.end()); vector <int> ans(q, -1); vector <int> queryIds(q); iota(queryIds.begin(), queryIds.end(), 0); divideAndConquer(0, n - 1, s, t, tRev, queryIds, queries, ans); return ans; } int main() { ios_base::sync_with_stdio(false); int n, m, q; cin >> n >> m >> q; string s, t; cin >> s >> t; vector <query> queries; for (int i = 0; i < q; i++) { int sl, sr, tl, tr; cin >> sl >> sr >> tl >> tr; sl--; sr--; tl--; tr--; queries.push_back({sl, sr, tl, tr}); } auto ans = solve(n, m, q, s, t, queries); for (int x : ans) { cout << x << '\n'; } return 0; } |