#pragma GCC optimize("O3") #pragma GCC optimize("Ofast") //#pragma GCC optimize("unroll-loops") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #include <bits/stdc++.h> using namespace std; // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // namespace debug { template <class c> struct rge { c b, e; }; template <class c> rge<c> range(c i, c j) { return rge<c>{i, j}; } template <class c> char elo(...); template <class c> auto elo(c* a) -> decltype(cerr << *a, 0); struct stream { ~stream() { cerr << endl; } template <class c> typename enable_if <sizeof elo<c>(0) != 1, stream&>::type operator<<(c i) { cerr << boolalpha << i; return *this; } template <class c> typename enable_if <sizeof elo<c>(0) == 1, stream&>::type operator<<(c i) { return *this << range(begin(i), end(i)); } template <class a, class b> stream& operator<<(pair <a,b> p) { return *this << "(" << p.first << ", " << p.second << ")"; } template <class c> stream& operator<<(rge<c> d) { *this << "["; for (auto it=d.b; it!=d.e; it++) *this << ", " + 2 * (it == d.b) << *it; return *this << "]"; } stream& _dbg(const string& s, int i, int b) {} template <class c, class ... cs> stream& _dbg(const string& s, int i, int b, c arg, cs ... args) { if (i == s.size()) return (*this << ": " << arg << ""); b += (s[i] == '(') + (s[i] == '[') + (s[i] == '{'); b -= (s[i] == ')') + (s[i] == ']') + (s[i] == '}'); if (s[i] == ',' && b == 0) return (*this << ": " << arg << " ")._dbg(s, i+1, b, args...); return (s[i] == ' ' ? *this : *this << s[i])._dbg(s, i+1, b, arg, args...); } };} #define dout debug::stream() #define dbg(...) ((dout << ">> ")._dbg(#__VA_ARGS__, 0, 0, __VA_ARGS__)) // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // #define f first #define s second #define sc scanf #define pr printf #define mp make_pair #define pb push_back #define all(x) x.begin(),x.end() #define ss(x) ((int)((x).size())) #define rep(i, a, b) for(int (i)=(a);i<=(b);(i)++) #define per(i, a, b) for(int (i)=(b);i>=(a);(i)--) #define lowb(v, x) (lower_bound(all(v),(x))-(v).begin()) #define uppb(v, x) (upper_bound(all(v),(x))-(v).begin()) #define upgrade ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0) #define erase_duplicates(x) {sort(all(x));(x).resize(distance((x).begin(), unique(all(x))));} template <class p, class q> pair<p,q> operator-(pair<p,q> a, pair<p,q> b) { return mp(a.f-b.f, a.s-b.s); } template <class p, class q> pair<p,q> operator+(pair<p,q> a, pair<p,q> b) { return mp(a.f+b.f, a.s+b.s); } template <class p, class q> void umin(p& a, const q& b) { if (a > b) a = b; } template <class p, class q> void umax(p& a, const q& b) { if (a < b) a = b; } using lf=long double; using ll=long long; using cll=const ll; using cint=const int; using pll=pair<ll,ll>; using pii=pair<int,int>; // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // const int Q = 100005; const int N = 3005; int out [Q]; int dp [N][N]; int par [N][N]; vector < pair < pair <int,int> , int> > vec [N][N]; inline void reroot (cint& root, cint& n, cint& m) { int i = root; int j = 1; while (j <= m && par[i][j] != 2) j++; par[i][j] = 1; while (i < n && j < m) { if (par[i+1][j] == 3) par[++i][j] = 1; else if (par[i+1][j+1] == 2) par[++i][++j] = 1; else j++; } while (i < n && par[i + 1][j] == 3) par[++i][j] = 1; } inline int query (int i, int j, cint& root) { int res = 0; while (i != root || j != 0) { if (par[i][j] == 1) j--; else if (par[i][j] == 2) { i--; j--; } else if (par[i][j] == 3) i--; res++; } return res; } void solve (char* A, char* B, int pos, int n, int m) { A[0] = B[0] = '#'; for (int i=0; i<=n; i++) for (int j=0; j<=m; j++) { dp[i][j] = 0; if (j == 0) { par[i][j] = 3; continue; } if (i == 0) { par[i][j] = 1; continue; } dp[i][j] = -1; if (dp[i][j] < dp[i][j-1]) { dp[i][j] = dp[i][j-1]; par[i][j] = 1; } if (dp[i][j] < dp[i-1][j-1] + (A[i] == B[j])) { dp[i][j] = dp[i-1][j-1] + (A[i] == B[j]); par[i][j] = 2; } if (dp[i][j] < dp[i-1][j]) { dp[i][j] = dp[i-1][j]; par[i][j] = 3; } } for (int i=1; i<=n; i++) { auto& v = vec[i][pos]; while (!v.empty()) { cint a = v.back().f.f; cint b = v.back().f.s - pos + 1; cint id = v.back().s; v.pop_back(); out[id] = a - (i - 1) + b - query(a, b, i - 1); } if (i < n) reroot(i, n, m); } } char A [N]; char B [N]; int main () { int n, m, q; scanf ("%d%d%d", &n, &m, &q); scanf ("%s", A + 1); scanf ("%s", B + 1); for (int i=1; i<=q; i++) { int l1, l2, r1, r2; scanf ("%d%d%d%d", &l1, &r1, &l2, &r2); vec[l1][l2].pb({{r1, r2}, i}); } for (int i=1; i<=m; i++) solve(A, B + i - 1, i, n + 1, m - i + 2); for (int i=1; i<=q; i++) printf ("%d\n", out[i]); 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 155 156 157 158 159 160 161 162 163 164 165 166 | #pragma GCC optimize("O3") #pragma GCC optimize("Ofast") //#pragma GCC optimize("unroll-loops") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #include <bits/stdc++.h> using namespace std; // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // namespace debug { template <class c> struct rge { c b, e; }; template <class c> rge<c> range(c i, c j) { return rge<c>{i, j}; } template <class c> char elo(...); template <class c> auto elo(c* a) -> decltype(cerr << *a, 0); struct stream { ~stream() { cerr << endl; } template <class c> typename enable_if <sizeof elo<c>(0) != 1, stream&>::type operator<<(c i) { cerr << boolalpha << i; return *this; } template <class c> typename enable_if <sizeof elo<c>(0) == 1, stream&>::type operator<<(c i) { return *this << range(begin(i), end(i)); } template <class a, class b> stream& operator<<(pair <a,b> p) { return *this << "(" << p.first << ", " << p.second << ")"; } template <class c> stream& operator<<(rge<c> d) { *this << "["; for (auto it=d.b; it!=d.e; it++) *this << ", " + 2 * (it == d.b) << *it; return *this << "]"; } stream& _dbg(const string& s, int i, int b) {} template <class c, class ... cs> stream& _dbg(const string& s, int i, int b, c arg, cs ... args) { if (i == s.size()) return (*this << ": " << arg << ""); b += (s[i] == '(') + (s[i] == '[') + (s[i] == '{'); b -= (s[i] == ')') + (s[i] == ']') + (s[i] == '}'); if (s[i] == ',' && b == 0) return (*this << ": " << arg << " ")._dbg(s, i+1, b, args...); return (s[i] == ' ' ? *this : *this << s[i])._dbg(s, i+1, b, arg, args...); } };} #define dout debug::stream() #define dbg(...) ((dout << ">> ")._dbg(#__VA_ARGS__, 0, 0, __VA_ARGS__)) // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // #define f first #define s second #define sc scanf #define pr printf #define mp make_pair #define pb push_back #define all(x) x.begin(),x.end() #define ss(x) ((int)((x).size())) #define rep(i, a, b) for(int (i)=(a);i<=(b);(i)++) #define per(i, a, b) for(int (i)=(b);i>=(a);(i)--) #define lowb(v, x) (lower_bound(all(v),(x))-(v).begin()) #define uppb(v, x) (upper_bound(all(v),(x))-(v).begin()) #define upgrade ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0) #define erase_duplicates(x) {sort(all(x));(x).resize(distance((x).begin(), unique(all(x))));} template <class p, class q> pair<p,q> operator-(pair<p,q> a, pair<p,q> b) { return mp(a.f-b.f, a.s-b.s); } template <class p, class q> pair<p,q> operator+(pair<p,q> a, pair<p,q> b) { return mp(a.f+b.f, a.s+b.s); } template <class p, class q> void umin(p& a, const q& b) { if (a > b) a = b; } template <class p, class q> void umax(p& a, const q& b) { if (a < b) a = b; } using lf=long double; using ll=long long; using cll=const ll; using cint=const int; using pll=pair<ll,ll>; using pii=pair<int,int>; // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // const int Q = 100005; const int N = 3005; int out [Q]; int dp [N][N]; int par [N][N]; vector < pair < pair <int,int> , int> > vec [N][N]; inline void reroot (cint& root, cint& n, cint& m) { int i = root; int j = 1; while (j <= m && par[i][j] != 2) j++; par[i][j] = 1; while (i < n && j < m) { if (par[i+1][j] == 3) par[++i][j] = 1; else if (par[i+1][j+1] == 2) par[++i][++j] = 1; else j++; } while (i < n && par[i + 1][j] == 3) par[++i][j] = 1; } inline int query (int i, int j, cint& root) { int res = 0; while (i != root || j != 0) { if (par[i][j] == 1) j--; else if (par[i][j] == 2) { i--; j--; } else if (par[i][j] == 3) i--; res++; } return res; } void solve (char* A, char* B, int pos, int n, int m) { A[0] = B[0] = '#'; for (int i=0; i<=n; i++) for (int j=0; j<=m; j++) { dp[i][j] = 0; if (j == 0) { par[i][j] = 3; continue; } if (i == 0) { par[i][j] = 1; continue; } dp[i][j] = -1; if (dp[i][j] < dp[i][j-1]) { dp[i][j] = dp[i][j-1]; par[i][j] = 1; } if (dp[i][j] < dp[i-1][j-1] + (A[i] == B[j])) { dp[i][j] = dp[i-1][j-1] + (A[i] == B[j]); par[i][j] = 2; } if (dp[i][j] < dp[i-1][j]) { dp[i][j] = dp[i-1][j]; par[i][j] = 3; } } for (int i=1; i<=n; i++) { auto& v = vec[i][pos]; while (!v.empty()) { cint a = v.back().f.f; cint b = v.back().f.s - pos + 1; cint id = v.back().s; v.pop_back(); out[id] = a - (i - 1) + b - query(a, b, i - 1); } if (i < n) reroot(i, n, m); } } char A [N]; char B [N]; int main () { int n, m, q; scanf ("%d%d%d", &n, &m, &q); scanf ("%s", A + 1); scanf ("%s", B + 1); for (int i=1; i<=q; i++) { int l1, l2, r1, r2; scanf ("%d%d%d%d", &l1, &r1, &l2, &r2); vec[l1][l2].pb({{r1, r2}, i}); } for (int i=1; i<=m; i++) solve(A, B + i - 1, i, n + 1, m - i + 2); for (int i=1; i<=q; i++) printf ("%d\n", out[i]); return 0; } |