#include <cstdio> #include <vector> #include <algorithm> #include <iostream> using namespace std; #define f first #define s second const int MAXN = 3e3 + 13; int dp[MAXN][MAXN]; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, m, z; int a, b, c, d; string s1, s2; cin >> n >> m >> z; cin >> s1 >> s2; while (z--) { cin >> a >> b >> c >> d; a--; b--; c--; d--; if (s1[a] == s2[c]) dp[a][c] = 1; else dp[a][c] = 0; for (int i = a + 1; i <= b; i++) { if (s1[i] == s2[c]) dp[i][c] = 1; else dp[i][c] = dp[i - 1][c]; } for (int i = c + 1; i <= d; i++) { if (s1[a] == s2[i]) dp[a][i] = 1; else dp[a][i] = dp[a][i - 1]; } for (int i = a + 1; i <= b; i++) { for (int j = c + 1; j <= d; j++) { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); if (s1[i] == s2[j]) dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 1); } } cout << dp[b][d] << '\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 | #include <cstdio> #include <vector> #include <algorithm> #include <iostream> using namespace std; #define f first #define s second const int MAXN = 3e3 + 13; int dp[MAXN][MAXN]; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, m, z; int a, b, c, d; string s1, s2; cin >> n >> m >> z; cin >> s1 >> s2; while (z--) { cin >> a >> b >> c >> d; a--; b--; c--; d--; if (s1[a] == s2[c]) dp[a][c] = 1; else dp[a][c] = 0; for (int i = a + 1; i <= b; i++) { if (s1[i] == s2[c]) dp[i][c] = 1; else dp[i][c] = dp[i - 1][c]; } for (int i = c + 1; i <= d; i++) { if (s1[a] == s2[i]) dp[a][i] = 1; else dp[a][i] = dp[a][i - 1]; } for (int i = a + 1; i <= b; i++) { for (int j = c + 1; j <= d; j++) { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); if (s1[i] == s2[j]) dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 1); } } cout << dp[b][d] << '\n'; } return 0; } |