#include <stdio.h> #include <stdbool.h> #include <string> #include <algorithm> #include <vector> #include <map> #include <inttypes.h> using namespace std; bool bPause = !true; int tab[3004][3004] = {0}; char ns[3004]; char ms[3004]; inline int max_v(int a, int b) { return a > b ? a : b; } int solve(int i, int j, int k, int l) { const int nn = j - i + 1; const int mm = l - k + 1; const int m = max_v(nn, mm) + 2; for (int id = 0; id < m; id++) tab[0][id] = 0; for (int id = 0; id < m; id++) tab[id][0] = 0; for (int y = 1; y <= nn; y++) for (int x = 1; x <= mm; x++) { if (ns[y - 1+i-1] == ms[x - 1+k-1]) { tab[y][x] = 1 + tab[y - 1][x - 1]; } else { tab[y][x] = max_v(tab[y - 1][x], tab[y][x - 1]); } } if (tab[nn][mm] < 0) { int g = 4; } return tab[nn][mm]; } int main() { int n,m,q; scanf("%d %d %d\n", &n, &m, &q); scanf("%s\n", &ns); scanf("%s\n", &ms); for (int i = 0; i < 3004; i++) tab[0][i] = 0; for (int i = 0; i < 3004; i++) tab[i][0] = 0; // abaab //b01112 //a11233 //b12234 //b13335 //a //a /* for (int y = 1; y <= n; y++) for (int x = 1; x <= m; x++) { if (ns[y - 1] == ms[x - 1]) { tab[y][x] = 1 + tab[y-1][x-1]; } else { tab[y][x] = max_v(tab[y - 1][x], tab[y][x - 1]); } } { printf(" "); for (int x = 1; x <= m; x++) { printf("%c ", ms[x - 1]); } printf("\n"); } for (int y = 1; y <= n; y++) { { printf("%c ", ns[y - 1]); } for (int x = 1; x <= m; x++) { printf("%d ", tab[y][x]); } printf("\n\n"); } */ for (int id = 0; id < q; id++) { int ii, jj, kk, ll; scanf("%d %d %d %d\n", &ii, &jj, &kk, &ll); //i--; j--; k--; l--; int s = solve(ii, jj,kk, ll); printf("%d\n",s); //printf("%d \n", tab[j][l] - tab[i][k]); } //printf("%d\n", tab[n][m]); #ifdef WIN32 if (bPause) while (true); #endif 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 | #include <stdio.h> #include <stdbool.h> #include <string> #include <algorithm> #include <vector> #include <map> #include <inttypes.h> using namespace std; bool bPause = !true; int tab[3004][3004] = {0}; char ns[3004]; char ms[3004]; inline int max_v(int a, int b) { return a > b ? a : b; } int solve(int i, int j, int k, int l) { const int nn = j - i + 1; const int mm = l - k + 1; const int m = max_v(nn, mm) + 2; for (int id = 0; id < m; id++) tab[0][id] = 0; for (int id = 0; id < m; id++) tab[id][0] = 0; for (int y = 1; y <= nn; y++) for (int x = 1; x <= mm; x++) { if (ns[y - 1+i-1] == ms[x - 1+k-1]) { tab[y][x] = 1 + tab[y - 1][x - 1]; } else { tab[y][x] = max_v(tab[y - 1][x], tab[y][x - 1]); } } if (tab[nn][mm] < 0) { int g = 4; } return tab[nn][mm]; } int main() { int n,m,q; scanf("%d %d %d\n", &n, &m, &q); scanf("%s\n", &ns); scanf("%s\n", &ms); for (int i = 0; i < 3004; i++) tab[0][i] = 0; for (int i = 0; i < 3004; i++) tab[i][0] = 0; // abaab //b01112 //a11233 //b12234 //b13335 //a //a /* for (int y = 1; y <= n; y++) for (int x = 1; x <= m; x++) { if (ns[y - 1] == ms[x - 1]) { tab[y][x] = 1 + tab[y-1][x-1]; } else { tab[y][x] = max_v(tab[y - 1][x], tab[y][x - 1]); } } { printf(" "); for (int x = 1; x <= m; x++) { printf("%c ", ms[x - 1]); } printf("\n"); } for (int y = 1; y <= n; y++) { { printf("%c ", ns[y - 1]); } for (int x = 1; x <= m; x++) { printf("%d ", tab[y][x]); } printf("\n\n"); } */ for (int id = 0; id < q; id++) { int ii, jj, kk, ll; scanf("%d %d %d %d\n", &ii, &jj, &kk, &ll); //i--; j--; k--; l--; int s = solve(ii, jj,kk, ll); printf("%d\n",s); //printf("%d \n", tab[j][l] - tab[i][k]); } //printf("%d\n", tab[n][m]); #ifdef WIN32 if (bPause) while (true); #endif return 0; } |