#include <bits/stdc++.h> using namespace std; #define N 3100 int n, m, q; int dp[N][N]; char s[N]; char t[N]; void solve() { int a, b, c, d; scanf("%d%d%d%d", &a, &b, &c, &d); for(int i=a-1; i<=b; ++i) { dp[i][c-1]=0; } for(int i=c-1; i<=d; ++i) { dp[a-1][i]=0; } for(int i=a; i<=b; ++i) { for(int j=c; j<=d; ++j) { dp[i][j]=max({dp[i-1][j], dp[i][j-1], dp[i-1][j-1]+(s[i]==t[j])}); } } printf("%d\n", dp[b][d]); } int main() { scanf("%d%d%d", &n, &m, &q); scanf("%s", s+1); scanf("%s", t+1); while(q--) { solve(); } 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 | #include <bits/stdc++.h> using namespace std; #define N 3100 int n, m, q; int dp[N][N]; char s[N]; char t[N]; void solve() { int a, b, c, d; scanf("%d%d%d%d", &a, &b, &c, &d); for(int i=a-1; i<=b; ++i) { dp[i][c-1]=0; } for(int i=c-1; i<=d; ++i) { dp[a-1][i]=0; } for(int i=a; i<=b; ++i) { for(int j=c; j<=d; ++j) { dp[i][j]=max({dp[i-1][j], dp[i][j-1], dp[i-1][j-1]+(s[i]==t[j])}); } } printf("%d\n", dp[b][d]); } int main() { scanf("%d%d%d", &n, &m, &q); scanf("%s", s+1); scanf("%s", t+1); while(q--) { solve(); } return 0; } |