#include <bits/stdc++.h> #define FOR(i, b, e) for (int i = (b); i <= (e); i++) #define FORD(i, b, e) for (int i = (e); i >= (b); i--) #define MP make_pair #define FS first #define ND second #define PB push_back #define ALL(x) x.begin(), x.end() using namespace std; using LL = long long; using PII = pair<int,int>; using PLL = pair<LL,LL>; using VI = vector<int>; using VLL = vector<LL>; template<class T> using V = vector<T>; template<class T> int sz(const T& a) { return (int)a.size(); } template<class T> void amin(T& a, T b) { a = min(a, b); } template<class T> void amax(T& a, T b) { a = max(a, b); } const int inf = 1e9; const LL infl = 1e18; char s[602], t[602]; int dp[601][601]; int n, m, q; int main() { scanf("%d %d %d", &n, &m, &q); scanf("%s %s", &s[1], &t[1]); while (q--) { int x0, x1, y0, y1; scanf("%d %d %d %d", &x0, &x1, &y0, &y1); memset(dp, 0, sizeof(dp)); int l1 = x1 - x0 + 1; int l2 = y1 - y0 + 1; FOR(i, 1, l1) FOR(j, 1, l2) { if (s[x0 + i - 1] == t[y0 + j - 1]) dp[i][j] = dp[i - 1][j - 1] + 1; else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } printf("%d\n", dp[l1][l2]); } }
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 | #include <bits/stdc++.h> #define FOR(i, b, e) for (int i = (b); i <= (e); i++) #define FORD(i, b, e) for (int i = (e); i >= (b); i--) #define MP make_pair #define FS first #define ND second #define PB push_back #define ALL(x) x.begin(), x.end() using namespace std; using LL = long long; using PII = pair<int,int>; using PLL = pair<LL,LL>; using VI = vector<int>; using VLL = vector<LL>; template<class T> using V = vector<T>; template<class T> int sz(const T& a) { return (int)a.size(); } template<class T> void amin(T& a, T b) { a = min(a, b); } template<class T> void amax(T& a, T b) { a = max(a, b); } const int inf = 1e9; const LL infl = 1e18; char s[602], t[602]; int dp[601][601]; int n, m, q; int main() { scanf("%d %d %d", &n, &m, &q); scanf("%s %s", &s[1], &t[1]); while (q--) { int x0, x1, y0, y1; scanf("%d %d %d %d", &x0, &x1, &y0, &y1); memset(dp, 0, sizeof(dp)); int l1 = x1 - x0 + 1; int l2 = y1 - y0 + 1; FOR(i, 1, l1) FOR(j, 1, l2) { if (s[x0 + i - 1] == t[y0 + j - 1]) dp[i][j] = dp[i - 1][j - 1] + 1; else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } printf("%d\n", dp[l1][l2]); } } |