#include <bits/stdc++.h> #define ll long long #define ld long double #define mp make_pair #define fi first #define se second #define pii pair <int,int> #define pli pair <ll,int> #define pll pair <ll,ll> #define ppi pair <pii,pii> #define vi vector <int> #define vl vector <ll> #define uset unordered_set #define umap unordered_map #define INF (1<<30) #define INF_LL (1ll<<60) #define MAX 2000005 #define MAX_LVL 21 #pragma GCC optimize("Ofast,unroll-loops,fast-math,O3") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx") //#pragma GCC target("avx,avx2,fma") //#pragma GCC optimization("unroll-loops") using namespace std; template <typename T> ostream & operator << (ostream &out, const vector <T> &V); template <typename T> ostream & operator << (ostream &out, const set <T> &S); template <typename T> ostream & operator << (ostream &out, const multiset <T> &S); template <typename T1, typename T2> ostream & operator << (ostream &out, const pair <T1, T2> &P); template <typename T1, typename T2> ostream & operator << (ostream &out, const map <T1, T2> &M); int T[3072][3072]; string a, b; void testcase_neon_1(int end_a, int end_b, int off_a=0, int off_b=0) { int p = end_a - off_a + 1, q = end_b - off_b + 1; for (int j = 0; j <= q; j++) { T[0][j] = 0; } for (int i = 1; i <= p; i++) { T[i][0] = 0; } for (int i = 1; i <= p; i++) { for (int j = 1; j <= q; j++) { if (a[off_a+i-1] == b[off_b+j-1]) { T[i][j] = T[i-1][j-1] + 1; } else { T[i][j] = T[i-1][j]; if (T[i][j] < T[i][j-1]) { T[i][j] = T[i][j-1]; } } } } } void testcase() { int n, m, q; cin >> n >> m >> q; cin >> a >> b; vector <ppi> Q(q); vector <int> Ans(q); umap <ll, vector<int> > M; for (int i = 0; i < q; i++) { cin >> Q[i].fi.fi >> Q[i].fi.se >> Q[i].se.fi >> Q[i].se.se; Q[i].fi.fi--, Q[i].fi.se--, Q[i].se.fi--, Q[i].se.se--; M[Q[i].fi.fi * (1ll<<30) + Q[i].se.fi].push_back(i); } for (auto &kv : M) { int off_a = 0, off_b = 0, end_a = off_a, end_b = off_b; for (int i : kv.se) { off_a = Q[i].fi.fi; off_b = Q[i].se.fi; end_a = max(end_a, Q[i].fi.se); end_b = max(end_b, Q[i].se.se); } testcase_neon_1(end_a, end_b, off_a, off_b); for (int i : kv.se) { Ans[i] = T[Q[i].fi.se-off_a+1][Q[i].se.se-off_b+1]; } } for (int a : Ans) { cout << a <<"\n"; } } void init() { } int main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); init(); int T = 1; //cin >> T; for (int t = 1; t <= T; t++) { testcase(); } return 0; } template <typename T> ostream & operator << (ostream &out, const vector <T> &V) { if (!V.empty()) { out << "{"; for (auto v : V) out << v << ", "; out << "\b\b}"; // \b is backspace } else { out << "{}"; } return out; } template <typename T> ostream & operator << (ostream &out, const set <T> &S) { if (!S.empty()) { out << "set{"; for (auto v : S) out << v << ", "; out << "\b\b}"; // \b is backspace } else { out << "{}"; } return out; } template <typename T> ostream & operator << (ostream &out, const multiset <T> &S) { if (!S.empty()) { out << "set{"; for (auto v : S) out << v << ", "; out << "\b\b}"; // \b is backspace } else { out << "{}"; } return out; } template <typename T1, typename T2> ostream & operator << (ostream &out, const map <T1,T2> &M) { if (!M.empty()) { out << "map{"; for (auto &kv : M) out << "(" << kv.fi << ": " << kv.se << ")" << ", "; out << "\b\b}"; // \b is backspace } else { out << "{}"; } return out; } template <typename T1, typename T2> ostream & operator << (ostream &out, const pair <T1, T2> &P) { out << "{" << P.first << ", " << P.second << "}"; return out; }
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 | #include <bits/stdc++.h> #define ll long long #define ld long double #define mp make_pair #define fi first #define se second #define pii pair <int,int> #define pli pair <ll,int> #define pll pair <ll,ll> #define ppi pair <pii,pii> #define vi vector <int> #define vl vector <ll> #define uset unordered_set #define umap unordered_map #define INF (1<<30) #define INF_LL (1ll<<60) #define MAX 2000005 #define MAX_LVL 21 #pragma GCC optimize("Ofast,unroll-loops,fast-math,O3") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx") //#pragma GCC target("avx,avx2,fma") //#pragma GCC optimization("unroll-loops") using namespace std; template <typename T> ostream & operator << (ostream &out, const vector <T> &V); template <typename T> ostream & operator << (ostream &out, const set <T> &S); template <typename T> ostream & operator << (ostream &out, const multiset <T> &S); template <typename T1, typename T2> ostream & operator << (ostream &out, const pair <T1, T2> &P); template <typename T1, typename T2> ostream & operator << (ostream &out, const map <T1, T2> &M); int T[3072][3072]; string a, b; void testcase_neon_1(int end_a, int end_b, int off_a=0, int off_b=0) { int p = end_a - off_a + 1, q = end_b - off_b + 1; for (int j = 0; j <= q; j++) { T[0][j] = 0; } for (int i = 1; i <= p; i++) { T[i][0] = 0; } for (int i = 1; i <= p; i++) { for (int j = 1; j <= q; j++) { if (a[off_a+i-1] == b[off_b+j-1]) { T[i][j] = T[i-1][j-1] + 1; } else { T[i][j] = T[i-1][j]; if (T[i][j] < T[i][j-1]) { T[i][j] = T[i][j-1]; } } } } } void testcase() { int n, m, q; cin >> n >> m >> q; cin >> a >> b; vector <ppi> Q(q); vector <int> Ans(q); umap <ll, vector<int> > M; for (int i = 0; i < q; i++) { cin >> Q[i].fi.fi >> Q[i].fi.se >> Q[i].se.fi >> Q[i].se.se; Q[i].fi.fi--, Q[i].fi.se--, Q[i].se.fi--, Q[i].se.se--; M[Q[i].fi.fi * (1ll<<30) + Q[i].se.fi].push_back(i); } for (auto &kv : M) { int off_a = 0, off_b = 0, end_a = off_a, end_b = off_b; for (int i : kv.se) { off_a = Q[i].fi.fi; off_b = Q[i].se.fi; end_a = max(end_a, Q[i].fi.se); end_b = max(end_b, Q[i].se.se); } testcase_neon_1(end_a, end_b, off_a, off_b); for (int i : kv.se) { Ans[i] = T[Q[i].fi.se-off_a+1][Q[i].se.se-off_b+1]; } } for (int a : Ans) { cout << a <<"\n"; } } void init() { } int main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); init(); int T = 1; //cin >> T; for (int t = 1; t <= T; t++) { testcase(); } return 0; } template <typename T> ostream & operator << (ostream &out, const vector <T> &V) { if (!V.empty()) { out << "{"; for (auto v : V) out << v << ", "; out << "\b\b}"; // \b is backspace } else { out << "{}"; } return out; } template <typename T> ostream & operator << (ostream &out, const set <T> &S) { if (!S.empty()) { out << "set{"; for (auto v : S) out << v << ", "; out << "\b\b}"; // \b is backspace } else { out << "{}"; } return out; } template <typename T> ostream & operator << (ostream &out, const multiset <T> &S) { if (!S.empty()) { out << "set{"; for (auto v : S) out << v << ", "; out << "\b\b}"; // \b is backspace } else { out << "{}"; } return out; } template <typename T1, typename T2> ostream & operator << (ostream &out, const map <T1,T2> &M) { if (!M.empty()) { out << "map{"; for (auto &kv : M) out << "(" << kv.fi << ": " << kv.se << ")" << ", "; out << "\b\b}"; // \b is backspace } else { out << "{}"; } return out; } template <typename T1, typename T2> ostream & operator << (ostream &out, const pair <T1, T2> &P) { out << "{" << P.first << ", " << P.second << "}"; return out; } |