#include <bits/stdc++.h> using namespace std; const int N = 3005; const int Q = 1e5 + 5; int n, m, q; char A[N], B[N]; int next1[26][N]; int next2[26][N]; int dp[N][N]; int dp2[N][N]; pair<int, int> p1[Q]; pair<int, int> p2[Q]; int res[Q]; void answer_small(int x) { if (p1[x].second - p1[x].first > p2[x].second - p2[x].first) { int max_res = p2[x].second - p2[x].first + 1; for (int i = 0; i <= max_res; i++) { dp[0][i] = p1[x].first - 1; } for (int i = 1; i <= max_res; i++) { for (int j = 1; j <= max_res; j++) { dp[i][j] = dp[i][j - 1]; if (dp[i - 1][j - 1] < p1[x].second) { dp[i][j] = min(dp[i][j], next1[B[p2[x].first + j - 2] - 'a'][dp[i - 1][j - 1]] + 1); } } } for (int i = max_res; i >= 0; i--) { if (dp[i][max_res] <= p1[x].second) { res[x] = i; return; } } } else { int max_res = p1[x].second - p1[x].first + 1; for (int i = 0; i <= max_res; i++) { dp2[0][i] = p2[x].first - 1; } for (int i = 1; i <= max_res; i++) { for (int j = 1; j <= max_res; j++) { dp2[i][j] = dp2[i][j - 1]; if (dp2[i - 1][j - 1] < p2[x].second) { // cout << i << " " << p1[x].first + j - 2 << " " << dp2[i - 1][j - 1] << " " << next2[A[p1[x].first + j - 2] - 'a'][dp2[i - 1][j - 1]] + 1 << endl; dp2[i][j] = min(dp2[i][j], next2[A[p1[x].first + j - 2] - 'a'][dp2[i - 1][j - 1]] + 1); } } } // for (int i = 0; i <= max_res; i++) { // for (int j = 0; j <= max_res; j++) { // printf("%d ", dp2[i][j]); // } // printf("\n"); // } for (int i = max_res; i >= 0; i--) { if (dp2[i][max_res] <= p2[x].second) { res[x] = i; return; } } } } int main() { scanf("%d%d%d", &n, &m, &q); scanf("%s", &A); scanf("%s", &B); for (int i = 0; i < 26; i++) { next1[i][n] = n; next2[i][m] = m; } for (int i = n - 1; i >= 0; i--) { for (int j = 0; j < 26; j++) { if (A[i] - 'a' == j) { next1[j][i] = i; } else { next1[j][i] = next1[j][i + 1]; } } } for (int i = 0; i <= n; i++) { for (int j = 0; j <= n; j++) { dp[i][j] = n + 1; } } for (int i = 0; i <= m; i++) { for (int j = 0; j <= m; j++) { dp2[i][j] = m + 1; } } for (int i = m - 1; i >= 0; i--) { for (int j = 0; j < 26; j++) { if (B[i] - 'a' == j) { next2[j][i] = i; } else { next2[j][i] = next2[j][i + 1]; } // cout << next2[j][i] << " "; } // cout << endl; } for (int i = 1; i <= q; i++) { scanf("%d%d%d%d", &p1[i].first, &p1[i].second, &p2[i].first, &p2[i].second); answer_small(i); printf("%d\n", res[i]); } }
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 | #include <bits/stdc++.h> using namespace std; const int N = 3005; const int Q = 1e5 + 5; int n, m, q; char A[N], B[N]; int next1[26][N]; int next2[26][N]; int dp[N][N]; int dp2[N][N]; pair<int, int> p1[Q]; pair<int, int> p2[Q]; int res[Q]; void answer_small(int x) { if (p1[x].second - p1[x].first > p2[x].second - p2[x].first) { int max_res = p2[x].second - p2[x].first + 1; for (int i = 0; i <= max_res; i++) { dp[0][i] = p1[x].first - 1; } for (int i = 1; i <= max_res; i++) { for (int j = 1; j <= max_res; j++) { dp[i][j] = dp[i][j - 1]; if (dp[i - 1][j - 1] < p1[x].second) { dp[i][j] = min(dp[i][j], next1[B[p2[x].first + j - 2] - 'a'][dp[i - 1][j - 1]] + 1); } } } for (int i = max_res; i >= 0; i--) { if (dp[i][max_res] <= p1[x].second) { res[x] = i; return; } } } else { int max_res = p1[x].second - p1[x].first + 1; for (int i = 0; i <= max_res; i++) { dp2[0][i] = p2[x].first - 1; } for (int i = 1; i <= max_res; i++) { for (int j = 1; j <= max_res; j++) { dp2[i][j] = dp2[i][j - 1]; if (dp2[i - 1][j - 1] < p2[x].second) { // cout << i << " " << p1[x].first + j - 2 << " " << dp2[i - 1][j - 1] << " " << next2[A[p1[x].first + j - 2] - 'a'][dp2[i - 1][j - 1]] + 1 << endl; dp2[i][j] = min(dp2[i][j], next2[A[p1[x].first + j - 2] - 'a'][dp2[i - 1][j - 1]] + 1); } } } // for (int i = 0; i <= max_res; i++) { // for (int j = 0; j <= max_res; j++) { // printf("%d ", dp2[i][j]); // } // printf("\n"); // } for (int i = max_res; i >= 0; i--) { if (dp2[i][max_res] <= p2[x].second) { res[x] = i; return; } } } } int main() { scanf("%d%d%d", &n, &m, &q); scanf("%s", &A); scanf("%s", &B); for (int i = 0; i < 26; i++) { next1[i][n] = n; next2[i][m] = m; } for (int i = n - 1; i >= 0; i--) { for (int j = 0; j < 26; j++) { if (A[i] - 'a' == j) { next1[j][i] = i; } else { next1[j][i] = next1[j][i + 1]; } } } for (int i = 0; i <= n; i++) { for (int j = 0; j <= n; j++) { dp[i][j] = n + 1; } } for (int i = 0; i <= m; i++) { for (int j = 0; j <= m; j++) { dp2[i][j] = m + 1; } } for (int i = m - 1; i >= 0; i--) { for (int j = 0; j < 26; j++) { if (B[i] - 'a' == j) { next2[j][i] = i; } else { next2[j][i] = next2[j][i + 1]; } // cout << next2[j][i] << " "; } // cout << endl; } for (int i = 1; i <= q; i++) { scanf("%d%d%d%d", &p1[i].first, &p1[i].second, &p2[i].first, &p2[i].second); answer_small(i); printf("%d\n", res[i]); } } |