#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma,tune=native")
typedef long long lld;
typedef double lf;
typedef long double llf;
typedef pair<int,int> pii;
typedef pair<lld,lld> pll;
#define For(i,s,a) for(int i = (int)s; i < (int)a; ++i)
#define rpt(s, it) for(auto it = s.begin(); it != s.end(); ++it)
#define brpt(s, it) for(auto it = s.rend(); it != s.rbegin(); --it)
#define sz size()
#define pb push_back
#define eb emplace_back
#define ff first
#define dd second
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define ZAPS {int t; scanf("%i", &t); while(t--) solve();}
#define make_unique(x) (x).erase( unique(all(x)), (x).end())
template<typename Ta, typename Tb>
ostream & operator <<(ostream & os, pair<Ta, Tb> x){
return os << x.ff << " " << x.dd;
}
char S[3001], s[3001];
char z[3001], Z[3001];
int dp[3001][3001];
const int inf = 0x3f3f3f3f;
int solve(int a, int b, int c, int d){
//cout<<a<<" "<<b<<" "<<c<<" "<<d<<endl;
memcpy(s, S + a, (b - a + 1));
memcpy(z, Z + c, (d - c + 1));
int x = b - a + 1;
int y = d - c + 1;
For(i, 0, x)
memset(dp[i], 0, y);
For(i, 0, x)
For(j, 0, y)
dp[i][j] = max(max(0, i ? dp[i - 1][j] : -inf), max(j ? dp[i][j - 1] : -inf, s[i] == z[j] ? (i && j) * dp[i - 1][j - 1] + 1 : -inf));
return dp[x - 1][y - 1];
}
int32_t main(void){
int n, m, q;
scanf("%d%d%d", &n, &m, &q);
scanf("%s", S);
scanf("%s", Z);
while(q--){
int a, b, c, d;
scanf("%d%d%d%d", &a, &b, &c, &d);
--a;--b;--c;--d;
printf("%d\n", solve(a,b,c,d));
}
}
/*
5 6 7
abaab
babbaa
1 5 1 6
1 3 2 4
2 5 2 5
1 4 2 5
2 5 3 6
2 2 5 6
3 4 2 2
*/
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 | #include <bits/stdc++.h> using namespace std; #pragma GCC optimize("Ofast") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma,tune=native") typedef long long lld; typedef double lf; typedef long double llf; typedef pair<int,int> pii; typedef pair<lld,lld> pll; #define For(i,s,a) for(int i = (int)s; i < (int)a; ++i) #define rpt(s, it) for(auto it = s.begin(); it != s.end(); ++it) #define brpt(s, it) for(auto it = s.rend(); it != s.rbegin(); --it) #define sz size() #define pb push_back #define eb emplace_back #define ff first #define dd second #define mp make_pair #define all(x) (x).begin(), (x).end() #define ZAPS {int t; scanf("%i", &t); while(t--) solve();} #define make_unique(x) (x).erase( unique(all(x)), (x).end()) template<typename Ta, typename Tb> ostream & operator <<(ostream & os, pair<Ta, Tb> x){ return os << x.ff << " " << x.dd; } char S[3001], s[3001]; char z[3001], Z[3001]; int dp[3001][3001]; const int inf = 0x3f3f3f3f; int solve(int a, int b, int c, int d){ //cout<<a<<" "<<b<<" "<<c<<" "<<d<<endl; memcpy(s, S + a, (b - a + 1)); memcpy(z, Z + c, (d - c + 1)); int x = b - a + 1; int y = d - c + 1; For(i, 0, x) memset(dp[i], 0, y); For(i, 0, x) For(j, 0, y) dp[i][j] = max(max(0, i ? dp[i - 1][j] : -inf), max(j ? dp[i][j - 1] : -inf, s[i] == z[j] ? (i && j) * dp[i - 1][j - 1] + 1 : -inf)); return dp[x - 1][y - 1]; } int32_t main(void){ int n, m, q; scanf("%d%d%d", &n, &m, &q); scanf("%s", S); scanf("%s", Z); while(q--){ int a, b, c, d; scanf("%d%d%d%d", &a, &b, &c, &d); --a;--b;--c;--d; printf("%d\n", solve(a,b,c,d)); } } /* 5 6 7 abaab babbaa 1 5 1 6 1 3 2 4 2 5 2 5 1 4 2 5 2 5 3 6 2 2 5 6 3 4 2 2 */ |
English