#include <cstdio> #include <vector> #include <algorithm> using namespace std; int main() { int n,m,q; char s[3013], t[3013]; scanf("%d %d %d %s %s",&n,&m,&q,s,t); while(q--) { int a,b,c,d; scanf("%d %d %d %d",&a,&b,&c,&d); a--,b--,c--,d--; vector<int> dp(m+1), e(m+1); for (int i = a; i <= b; i++) { for (int j = c; j <= d; j++) { dp[j+1] = std::max(dp[j+1], dp[j]); e[j+1] = std::max(dp[j+1], dp[j] + (s[i] == t[j] ? 1 : 0)); } dp = e; } for (int j = c; j <= d; j++) dp[j+1] = std::max(dp[j+1], dp[j]); printf("%d\n", dp[d+1]); } 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 | #include <cstdio> #include <vector> #include <algorithm> using namespace std; int main() { int n,m,q; char s[3013], t[3013]; scanf("%d %d %d %s %s",&n,&m,&q,s,t); while(q--) { int a,b,c,d; scanf("%d %d %d %d",&a,&b,&c,&d); a--,b--,c--,d--; vector<int> dp(m+1), e(m+1); for (int i = a; i <= b; i++) { for (int j = c; j <= d; j++) { dp[j+1] = std::max(dp[j+1], dp[j]); e[j+1] = std::max(dp[j+1], dp[j] + (s[i] == t[j] ? 1 : 0)); } dp = e; } for (int j = c; j <= d; j++) dp[j+1] = std::max(dp[j+1], dp[j]); printf("%d\n", dp[d+1]); } return 0; } |