#include <bits/stdc++.h> #include <math.h> using namespace std; #define endl "\n" #define mp make_pair #define st first #define nd second #define pii pair<int, int> #define pb push_back #define _upgrade ios_base::sync_with_stdio(0), cout.setf(ios::fixed), cout.precision(10), cin.tie(0), cout.tie(0); #define REP(i, n) for (int i = 0; i < (n); ++i) #define FWD(i, a, b) for (int i = (a); i < (b); ++i) #define rep(i, n) for (int32_t i = 0; i < (n); ++i) #define fwd(i, a, b) for (int i = (a); i < (b); ++i) #define all(c) (c).begin(), (c).end() #define sz(X) (int)((X).size()) #define what(x) cerr << #x << " is " << x << endl; ostream &operator<<(ostream &out, string str) { for (char c : str) out << c; return out; } template <class L, class R> ostream &operator<<(ostream &out, pair<L, R> p) { return out << "(" << p.st << ", " << p.nd << ")"; } template <class T> auto operator<<(ostream &out, T a) -> decltype(a.begin(), out) { out << '{'; for (auto it = a.begin(); it != a.end(); it = next(it)) out << (it != a.begin() ? ", " : "") << *it; return out << '}'; } void dump() { cerr << "\n"; } template <class T, class... Ts> void dump(T a, Ts... x) { cerr << a << ", "; dump(x...); } #define DEBUG false #define debug(...) \ if (DEBUG) \ cerr << "[" #__VA_ARGS__ "]: ", dump(__VA_ARGS__) const int MAXN = 3003; const int MAXQ = 1e5 + 99; int tab1[MAXN][MAXN]; int tab2[MAXN][MAXN]; long long n, m, q; string A, B; int lcs(const string &a, const string &b, int tab[MAXN][MAXN]) { rep(i, sz(a)) rep(j, sz(b)) { tab[i + 1][j + 1] = (a[i] == b[j]) ? tab[i][j] + 1 : max(tab[i][j + 1], tab[i + 1][j]); } return tab[sz(a)][sz(b)]; } ///////////////////////////////////////////////////////////////// int solve_brut(int a, int b, int c, int d) { auto r1 = A.substr(a, b - a + 1); auto r2 = B.substr(c, d - c + 1); return lcs(r1, r2, tab1); } void brut() { cin >> A >> B; rep(j, q) { int a, b, c, d; cin >> a >> b >> c >> d; --a, --b, --c, --d; cout << solve_brut(a, b, c, d) << endl; } } ///////////////////////////////////////////////////////////////////////// int wynik[MAXQ]; const int MAXM = 3000; const int inf = 4 * MAXN; inline int get(int a, int b, int tab[MAXN][MAXN]) { a++, b++; return (a < 0 || b < 0) ? -inf : tab[a][b]; } void solve_better(string A, string B, vector<vector<int>> X) { if (sz(X) == 0) return; if (sz(A) > sz(B)) { swap(A, B); for (auto &x : X) swap(x[0], x[2]), swap(x[1], x[3]); } int s = sz(B) / 2; string B1 = B.substr(0, s); string B2 = B.substr(s); vector<vector<int>> X1; vector<vector<int>> X2; vector<vector<int>> cur; for (auto &x : X) { if (x[3] < s) { X1.push_back(x); } else if (x[2] >= s) { x[2] -= s; x[3] -= s; X2.push_back(x); } else cur.push_back(x); } reverse(all(B1)); // debug(A, B); if (sz(cur) != 0) for (int i = 0; i <= sz(A); i++) { // for (int l = 0; l <= max(sz(A), sz(B)) + 1; l++) // for (int k = 0; k <= max(sz(A), sz(B)) + 1; k++) // tab1[k][l] = tab2[k][l] = 0; auto A1 = A.substr(0, i); auto A2 = A.substr(i); reverse(all(A1)); lcs(A1, B1, tab1); lcs(A2, B2, tab2); // debug(A1, B1, A2, B2); for (auto &q : cur) { int d1 = get(i - 1 - q[0], s - q[2] - 1, tab1); int d2 = get(q[1] - i, q[3] - s, tab2); // debug(q, d1, d2); wynik[q[4]] = max(wynik[q[4]], d1 + d2); } } reverse(all(B1)); B.clear(); X.clear(); cur.clear(); solve_better(A, B1, X1); solve_better(A, B2, X2); } void better() { cin >> A >> B; vector<vector<int>> X; X.reserve(q); rep(j, q) { int a, b, c, d; cin >> a >> b >> c >> d; --a, --b, --c, --d; if ((b - a + 1) * (d - c + 1) <= MAXM) wynik[j] = solve_brut(a, b, c, d); else { X.push_back({a, b, c, d, j}); } } solve_better(A, B, X); rep(j, q) cout << wynik[j] << endl; // cout << endl; } void mock() { A.resize(n, 'a'); B.resize(m, 'a'); vector<vector<int>> X; rep(j, q) { int a, b, c, d; a = 1 + rand() % n; b = 1 + rand() % n; c = 1 + rand() % m; d = 1 + rand() % m; if (a > b) swap(a, b); if (c > d) swap(c, d); --a, --b, --c, --d; if ((b - a) * (d - c) <= MAXM) wynik[j] = solve_brut(a, b, c, d); else { X.push_back({a, b, c, d, j}); } } solve_better(A, B, X); rep(j, q) cout << wynik[j] << endl; } int32_t main() { _upgrade; cin >> n >> m >> q; // brut(); // mock(); better(); }
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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 | #include <bits/stdc++.h> #include <math.h> using namespace std; #define endl "\n" #define mp make_pair #define st first #define nd second #define pii pair<int, int> #define pb push_back #define _upgrade ios_base::sync_with_stdio(0), cout.setf(ios::fixed), cout.precision(10), cin.tie(0), cout.tie(0); #define REP(i, n) for (int i = 0; i < (n); ++i) #define FWD(i, a, b) for (int i = (a); i < (b); ++i) #define rep(i, n) for (int32_t i = 0; i < (n); ++i) #define fwd(i, a, b) for (int i = (a); i < (b); ++i) #define all(c) (c).begin(), (c).end() #define sz(X) (int)((X).size()) #define what(x) cerr << #x << " is " << x << endl; ostream &operator<<(ostream &out, string str) { for (char c : str) out << c; return out; } template <class L, class R> ostream &operator<<(ostream &out, pair<L, R> p) { return out << "(" << p.st << ", " << p.nd << ")"; } template <class T> auto operator<<(ostream &out, T a) -> decltype(a.begin(), out) { out << '{'; for (auto it = a.begin(); it != a.end(); it = next(it)) out << (it != a.begin() ? ", " : "") << *it; return out << '}'; } void dump() { cerr << "\n"; } template <class T, class... Ts> void dump(T a, Ts... x) { cerr << a << ", "; dump(x...); } #define DEBUG false #define debug(...) \ if (DEBUG) \ cerr << "[" #__VA_ARGS__ "]: ", dump(__VA_ARGS__) const int MAXN = 3003; const int MAXQ = 1e5 + 99; int tab1[MAXN][MAXN]; int tab2[MAXN][MAXN]; long long n, m, q; string A, B; int lcs(const string &a, const string &b, int tab[MAXN][MAXN]) { rep(i, sz(a)) rep(j, sz(b)) { tab[i + 1][j + 1] = (a[i] == b[j]) ? tab[i][j] + 1 : max(tab[i][j + 1], tab[i + 1][j]); } return tab[sz(a)][sz(b)]; } ///////////////////////////////////////////////////////////////// int solve_brut(int a, int b, int c, int d) { auto r1 = A.substr(a, b - a + 1); auto r2 = B.substr(c, d - c + 1); return lcs(r1, r2, tab1); } void brut() { cin >> A >> B; rep(j, q) { int a, b, c, d; cin >> a >> b >> c >> d; --a, --b, --c, --d; cout << solve_brut(a, b, c, d) << endl; } } ///////////////////////////////////////////////////////////////////////// int wynik[MAXQ]; const int MAXM = 3000; const int inf = 4 * MAXN; inline int get(int a, int b, int tab[MAXN][MAXN]) { a++, b++; return (a < 0 || b < 0) ? -inf : tab[a][b]; } void solve_better(string A, string B, vector<vector<int>> X) { if (sz(X) == 0) return; if (sz(A) > sz(B)) { swap(A, B); for (auto &x : X) swap(x[0], x[2]), swap(x[1], x[3]); } int s = sz(B) / 2; string B1 = B.substr(0, s); string B2 = B.substr(s); vector<vector<int>> X1; vector<vector<int>> X2; vector<vector<int>> cur; for (auto &x : X) { if (x[3] < s) { X1.push_back(x); } else if (x[2] >= s) { x[2] -= s; x[3] -= s; X2.push_back(x); } else cur.push_back(x); } reverse(all(B1)); // debug(A, B); if (sz(cur) != 0) for (int i = 0; i <= sz(A); i++) { // for (int l = 0; l <= max(sz(A), sz(B)) + 1; l++) // for (int k = 0; k <= max(sz(A), sz(B)) + 1; k++) // tab1[k][l] = tab2[k][l] = 0; auto A1 = A.substr(0, i); auto A2 = A.substr(i); reverse(all(A1)); lcs(A1, B1, tab1); lcs(A2, B2, tab2); // debug(A1, B1, A2, B2); for (auto &q : cur) { int d1 = get(i - 1 - q[0], s - q[2] - 1, tab1); int d2 = get(q[1] - i, q[3] - s, tab2); // debug(q, d1, d2); wynik[q[4]] = max(wynik[q[4]], d1 + d2); } } reverse(all(B1)); B.clear(); X.clear(); cur.clear(); solve_better(A, B1, X1); solve_better(A, B2, X2); } void better() { cin >> A >> B; vector<vector<int>> X; X.reserve(q); rep(j, q) { int a, b, c, d; cin >> a >> b >> c >> d; --a, --b, --c, --d; if ((b - a + 1) * (d - c + 1) <= MAXM) wynik[j] = solve_brut(a, b, c, d); else { X.push_back({a, b, c, d, j}); } } solve_better(A, B, X); rep(j, q) cout << wynik[j] << endl; // cout << endl; } void mock() { A.resize(n, 'a'); B.resize(m, 'a'); vector<vector<int>> X; rep(j, q) { int a, b, c, d; a = 1 + rand() % n; b = 1 + rand() % n; c = 1 + rand() % m; d = 1 + rand() % m; if (a > b) swap(a, b); if (c > d) swap(c, d); --a, --b, --c, --d; if ((b - a) * (d - c) <= MAXM) wynik[j] = solve_brut(a, b, c, d); else { X.push_back({a, b, c, d, j}); } } solve_better(A, B, X); rep(j, q) cout << wynik[j] << endl; } int32_t main() { _upgrade; cin >> n >> m >> q; // brut(); // mock(); better(); } |